英文:
how can iterate and populate the writeFields() arguments, with a hashmap
问题
你可以尝试使用HashMap来更好地填充setUp
方法中的writeField()
。以下是一个示例:
public class testing {
@InjectMocks
private Test test;
@Before
public void setUp() throws IllegalAccessException {
HashMap<String, String> fieldValues = new HashMap<>();
fieldValues.put("x1", "a");
fieldValues.put("x2", "b");
fieldValues.put("x3", "/c");
fieldValues.put("x4", "d");
fieldValues.put("x5", "e");
fieldValues.put("x6", "f");
for (Map.Entry<String, String> entry : fieldValues.entrySet()) {
FieldUtils.writeField(test, entry.getKey(), entry.getValue(), true);
}
}
}
这样,你可以使用循环遍历HashMap来填充writeField()
,使代码更简洁和可维护。
英文:
I have this code, it is a junit test class. I want to come up with a better way to populate writeField() inside the setUP method.
Can i use a hashmap? any examples
public class testing {
@InjectMocks
private Test test;
@Before
public void setUp() throws IllegalAccessException {
FieldUtils.writeField(test, "x1", "a", true);
FieldUtils.writeField(test, "x2", "b", true);
FieldUtils.writeField(test, "x3", "/c", true);
FieldUtils.writeField(test, "x4", "d", true);
FieldUtils.writeField(test, "x5", "e", true);
FieldUtils.writeField(test, "x6", "f", true);
}
}
basically having FieldUtils.writeField inside a for loop and have populate.
答案1
得分: 1
err, um.. yes?
Map<String, String> myStuff = new HashMap<String, String>();
map.put("subNavLabel", "Marketing");
// ... more map put statements
for (var entry : map.entySet()) {
FieldUtils.writeField(test, entry.getKey(), entry.getValue(), true);
}
这似乎对我来说是非常基础的Java,毫无疑问,这个简单的答案回应了您的问题,如所述。如果不足够,请考虑更新您的问题或提出一个更详细的问题。
英文:
err, um.. yes?
Map<String, String> myStuff = new HashMap<String, String>();
map.put("subNavLabel", "Marketing");
// ... more map put statements
for (var entry : map.entySet()) {
FieldUtils.writeField(test, entry.getKey(), entry.getValue(), true);
}
This seems extremely first steps java to me, surely this trivial answer isn't what you want, but it answers the question as stated. If it doesn't suffice, consider updating your question or asking a new one with (a lot) more detail.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论