Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mock instance is null after @Mock annotation

I try to run this test:

    @Mock IRoutingObjHttpClient routingClientMock;     @Mock IRoutingResponseRepository routingResponseRepositoryMock;       @Test     public void testSendRoutingRequest() throws Exception {         CompleteRoutingResponse completeRoutingResponse = new CompleteRoutingResponse();         completeRoutingResponse.regression_latencyMillis = 500L;          Mockito.when(routingClientMock.sendRoutingRequest(any(RoutingRequest.class))).thenReturn(completeRoutingResponse);          RoutingObjHttpClientWithReRun routingObjHttpClientWithReRun = new RoutingObjHttpClientWithReRun                 (routingClientMock, routingResponseRepositoryMock);  ...     } 

but I get NullPointerException for:

Mockito.when(routingClientMock.

what am i missing?

like image 226
Elad Benda2 Avatar asked Apr 12 '15 14:04

Elad Benda2


People also ask

Why is mock returning null object?

RETURNS_MOCKS. If a method return type is a custom class, a mock returns null because there is no empty value for a custom class. RETURN_MOCKS will try to return mocks if possible instead of null . Since final class cannot be mocked, null is still returned in that case.

What does @mock annotation mean?

Using the @Mock annotation – allows shorthand creation of objects required for testing. minimizes repetitive mock creation code. makes the test class more readable. makes the verification error easier to read because field name is used to identify the mock.

What does @mock do in Mockito?

Spring Boot's @MockBean Annotation We can use the @MockBean to add mock objects to the Spring application context. The mock will replace any existing bean of the same type in the application context. If no bean of the same type is defined, a new one will be added.

What is difference between @mock and Mockito mock?

Using an annotation ( @Mock ) is usually considered "cleaner", as you don't fill up your code with boilerplate assignments that all look the same. Note that in order to use the @Mock annotation, your test class should be annotated with @RunWith(MockitoJUnitRunner. class) or contain a call to MockitoAnnotations.


2 Answers

When you want to use the @Mock annotation you should use the MockitoJUnitRunner

@RunWith(MockitoJUnitRunner.class) public class MockitoTest {      @Mock     private IRoutingObjHttpClient routingClientMock;      @Test     public void testSendRoutingRequest() throws Exception {         // ...     }  } 

See also this tutorial.

like image 141
Roland Weisleder Avatar answered Sep 21 '22 02:09

Roland Weisleder


You have three options for activating the @Mock annotation: MockitoRule, MockitoJUnitRunner, MockitoAnnotations.initMocks(this). IMHO using the MockitoRule is the best one, because it lets you still choose another runner like e.g. Parameterized.

Use the MockitoRule

public class MockitoTest {    @Mock   private IRoutingObjHttpClient routingClientMock;    @Rule   public MockitoRule rule = MockitoJUnit.rule();    @Test   public void testSendRoutingRequest() throws Exception {     // ...   } } 

Use the MockitoJUnitRunner

@RunWith(MockitoJUnitRunner.class) public class MockitoTest {    @Mock   private IRoutingObjHttpClient routingClientMock;    @Test   public void testSendRoutingRequest() throws Exception {     // ...   } } 

Call MockitoAnnotations.initMocks(this) explicitly.

This can be done in qn @Before method, in your own runner or in an own rule.

public class MockitoTest {    @Mock   private IRoutingObjHttpClient routingClientMock;    @Before   public void createMocks() {     MockitoAnnotations.initMocks(this);   }    @Test   public void testSendRoutingRequest() throws Exception {     // ...   } } 
like image 29
Stefan Birkner Avatar answered Sep 25 '22 02:09

Stefan Birkner