I stuck since a couple of hours on a test method.
I tried to reproduce a similar example. I have a service who extend a abstract service with an utility method like this:
public class MyService extends MyAbstractService {
@Autowired
private UserRepository userRepository;
public void whatever(MyDTO myDTO) {
User user = this.userRepository.findByName(myDTO.userId);
hello(user.name);
}
}
abstract class MyAbstractService {
protected void hello(String userName) {
System.out.printf("Hello %s", userName);
}
}
my testing class :
@Test
void whenIcallWhaterver() {
MyService myService = Mockito.mock(MyService.class, InvocationOnMock::callRealMethod);
myService.whatever(myDTO);
verify(myService, only()).hello(anyString());
}
My goal is just to verify if when I go into the method whatever, the method of the abstract service is called too. I got a null pointer exception because the repository isn't not init in the mock (normal behavior I assume), but I would like to learn/understand how test this.
How could I do to fix this?
Thanks for your help
What I have tried:
I tried another way where I don't use directly the repository into the tested service and user when theReturn but I assume I could inject the repository into the mocked service maybe?