So basically, I am trying to write a unit test but my question is do we need to mock every single argument of variable/object? Doesn't this simply defeat the purpose by manually putting the correct values.
public class MainClass{
public void somefunction(Object obj){
List<someOtherClass> lists = obj.list;
for (someOtherClass list:lists){
System.out.println(list.v1+list.v2);
}
}
}
public class SomeClass {
public String id;
public List<SomeOtherClass> list;
}
public class SomeOtherClass {
public String v1;
public String v2;
}
In my test file:
class MainClassTest {
List<SomeOtherClass> list = new ArrayList<>();
SomeOtherClass list1 = spy(new SomeOtherClass()); // as adviced here: https://stackoverflow.com/questions/33125769/mockito-when-thenreturn-doesnt-work-properly
@Test
void someFunction(){
when(list1.v1).thenReturn("v1");
// When I do the above line and try to print `list1.v1` I still get null value.
}
}
- Should I just mock v1 and v2 and then create the list? Which is the correct and more efficient way? I actually have lot of variables and passing
null
to check NPE is going to be a pain.