Click here to Skip to main content
15,616,432 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 {
        private UserRepository userRepository;
        public void whatever(MyDTO myDTO) {
            User user = this.userRepository.findByName(myDTO.userId);
    abstract class MyAbstractService {
        protected void hello(String userName) {
            System.out.printf("Hello %s", userName);

my testing class :

    void whenIcallWhaterver() {
        MyService myService = Mockito.mock(MyService.class, InvocationOnMock::callRealMethod);
        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?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900