尝试在library 的帮助下使用我自己的Enum
喜欢。但问题是我无法从对象中获取正确的开关循环工作的价值。mock
mockneat
string
MockUnit
这是我的Enum
:
public enum SubType {
ROUTER("router"),
SERVER("server"),
private final String value;
SubType(final String newValue) {
value = newValue;
}
public String getValue() {
return value;
}
}
这是我的Build
课,我尝试使用以下自定义值SubType
:
public class Build {
private MockNeat mockNeat = MockNeat.threadLocal();
private ArrayList<String> group = new ArrayList<>();
public void addToList() {
group.add("All Devices");
group.add("All Groups");
for (i = 0; i < 10; i++) {
MockUnit subType = mockNeat.from(SubType.class).map(SubType::getValue);
//EDITED:
//For example there are sout(subType) = "router"
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = mockNeat.reflect(Build.class)
.field("group", constructGroupField(subType, group))
.map(gson::toJson)
.val();
System.out.println(json);
}
}
private ArrayList<String> constructGroupField(MockUnit subType, ArrayList<String> group) {
//System.out.println(subType);
//Output: net.andreinc.mockneat.abstraction.MockUnit$$Lambda$22/902830499@740773a3
//But how I can get there my string like "router" or ""server"
//How I can cast MockUnit to String value?
//EDITED:
// And there it can be already sout(subType)= "server"
switch (subType.toString()) {
case "router":
group.add("All AAA");
break;
case "server":
group.add("All BBB");
break;
}
return group;
}
}
主要目标是,根据subtype
字段中的字符串,输出相应的json
文件
更新:
Json
我要生成的最终文件:
{
"product": "a",
"group": "router"
}
另一种情况:
private String someMethod() {
MockUnitInt localId = mockNeat.ints().range(5000, 35_000);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
return mockNeat.reflect(someEntity.class)
.field("deviceLocalID", localId.mapToString())
.field("localID", localId.mapToString())
.map(gson::toJson)
.val();
}
即使localId
是类中的一个final
字段,当有东西引用它时,它每次都会给出随机值。不明白这有什么问题。。