This is the (simplified) method that I need to write a unit test case in mockito, however I get a null pointer exception:
public class Class1 {
@Autowire
AutoWiredObject autoWiredObject;
public Object1 getAccount(boolean b1) {
Object1 object = new Object1();
autoWiredObject.setAllValues(object); //object.get("value") is set to something
//here. Also this is where the exception follows when I test the unit test method
return object;
}
}
I tried something like this in the test class:
@InjectMocks
Class1 myClass;
@Spy // I researched this is how we solve the issue of autowired
AutoWiredObject autoWiredObject = new AutoWiredObject();
@Test
public void testGetAccount() {
Object1 object = myClass.getAccount(true); //this is where the null pointer exception
//starts
assertTrue(object.get("value") != null);
}
What is the best way to unit test this type of methods? by the way, if I check the implementation of the AutowiredObject class, the "setAllValues" methods also contains an autowired object inside calling a method, so my point is, how to deal with this "chain" of autowired??
Or is there another easier way for me to test the line
autowiredObject.setAllValues(object)
??