Am using Mockito version 3.6.28 for Junit testing. Am getting Nullpointer Exception while calling the real method on the object. Its because of the dependency on the target object is not injected correctly .This is the code am using.
public class ClassA{
@Autowired
LoggingService loggingService;
@Autowired
ClassB classB;
publc void doSomething(){
loggingService.info("info log"); // This will works fine
classB.doSomething();
}
}
public class ClassB{
@Autowired
LoggingService loggingService;
public void doSomething(){
loggingService.info("info log"); // Nullpointer on this line since loggingService is null
}
}
@RunWith(MockitoJUnitRunner.Silent.class)
public class TestClass{
@InjectMocks
ClassA classA;
@Mock
ClassB classB;
@Mock
private LoggingService loggingService;
@Test
public void testMethod(){
doCallRealMethod().when(classB).doSomething();
classA.doSomething();
}
}