Rahul Bhawar
Rahul Bhawar

Reputation: 450

Mockito method call stub not working - Mockito.doReturn(false).when(studentServiceImpl).myClass().isValidUser(ArgumentMatchers.anyInt());

adding test cases for getStudent method, this is having internal calls

1- is repository call - stubbing this call working fine

2- validate user call - stubbing this call not working, showing some error and test case failed.

Service Class

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    FakeStudentRepository fakeStudentRepository;
    @Override
    public Optional<Student> getStudent(int id) {
        Optional<Student> student = fakeStudentRepository.getStudent(id);
        boolean isValid = myClass().isValidUser(student.get().getId());
        if(!isValid) {
            return Optional.empty();
        }
        return student;
    }
    public MyTestClass myClass() {
        return new MyTestClass();
    }
 }

MyTestClass

public class MyTestClass {
    public boolean isValidUser(int id) {
        return true;
    }
}

Test Class

@SpringBootTest
class StudentServiceImplTest {

    @Mock
    FakeStudentRepository fakeStudentRepository;

    @InjectMocks
    StudentServiceImpl studentServiceImpl;

    @BeforeEach
    public void setup() {
        studentServiceImpl = Mockito.spy(StudentServiceImpl.class);
        MockitoAnnotations.initMocks(this);
    }

    @Test
    void getStudent() {
        Optional<Student> student = Optional.of(Student.builder().id(1).firstName("Rahul").lastName("rahul")
                .mobile("XXXXXX").build());

        Mockito.doReturn(student)
                .when(fakeStudentRepository).getStudent(ArgumentMatchers.anyInt());

        Mockito.doReturn(false)
                .when(studentServiceImpl).myClass().isValidUser(ArgumentMatchers.anyInt());

        Optional<Student> resultStudent = studentServiceImpl.getStudent(student.get().getId());
        assertEquals(resultStudent.get().getId(), student.get().getId());
    }
}

Error

org.mockito.exceptions.misusing.WrongTypeOfReturnValue: Boolean cannot be returned by myClass() myClass() should return MyTestClass

If you're unsure why you're getting above error read on. Due to the nature of the syntax above problem might occur because: 1. This exception might occur in wrongly written multi-threaded tests. Please refer to Mockito FAQ on limitations of concurrency testing. 2. A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies - - with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() method.

Upvotes: 0

Views: 581

Answers (1)

johanneslink
johanneslink

Reputation: 5361

The error Message says it: You are mocking studentServiceImpl.myClass() and try to return true. It’s not possible to mock the end of a call chain as you try with your second Mockito expression.

To do what you want requires to mock myClass() first by returning a mocked class instance and mock isValidUser on that.

Upvotes: 1

Related Questions