Issue
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.
Solution
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.
Answered By - rzwitserloot