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?
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.
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.
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.
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.
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.
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
.
public class MockitoTest { @Mock private IRoutingObjHttpClient routingClientMock; @Rule public MockitoRule rule = MockitoJUnit.rule(); @Test public void testSendRoutingRequest() throws Exception { // ... } }
@RunWith(MockitoJUnitRunner.class) public class MockitoTest { @Mock private IRoutingObjHttpClient routingClientMock; @Test public void testSendRoutingRequest() throws Exception { // ... } }
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 { // ... } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With