I'm facing to a similar kind of scenario as described below. Since I'm unable to share the actual code I'm using that.
I'm trying to test the addToList()
method in the below class.
public class MyClass{
private List<String> nameList = new ArrayList<String>();
public void addToList(String name){
nameList.add(name);
}
}
I want to check whether the list get updated by calling the addToList()
method. (nameList.size()
check is enough)
I'm writing JUnits and can't figure out a way to access that nameList
after calling the method.
@Test
public void test() {
MyClass my = new MyClass();
my.addToList("one");
my.addToList("two");
// how to check the 'nameList' to complete the test
}
May be Reflections can be the solution for this, but still it is unclear.
What is the best way to do that (without changing the actual class).