I'm trying to mock the following piece of code in my JUnit test
requestData = requestRepository.findByRequestId(requestId);
by doing
@Mock
RequestRepository requestRepository;
@Mock
RequestData requestData;
Mockito.when(requestRepository.findByRequestId(requestId)).thenReturn(requestData);
But instead of giving the mock object back, i'm getting null value. What is the correct way to mock MongoDB repository methods.
requestIdvalue during test execution is not the same as the one used to mock call.@Mockwill work only if you callMockitoAnnotations.initMocks(this);or if you are using@RunWith(MockitoJUnitRunner.class). If your test is going directly to the db is because probably you are running withSpringRunnerwhich is starting your full application, and this is actually an integration test