Reputation: 15472
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?
Upvotes: 151
Views: 205896
Reputation: 11
My Code
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
<version>5.0.0</version>
</dependency>
Solution
Change @Test
to import from -
import org.junit.jupiter.api.Test;
instead of -
import org.junit.Test;
Upvotes: 1
Reputation: 625
our application uses JUNIT5
, same issue occured.
I replaced @Mock
with @InjectMocks
- then issue was resolved
Upvotes: 0
Reputation: 1738
Junit 5 with InjectMocks. Based on above suggestion https://stackoverflow.com/a/55616702/2643815
@ExtendWith(MockitoExtension.class)
public class MyClientTest {
@Mock
Environment environment;
@Mock
ClassCreator classCreator;
@InjectMocks
MyClient myClient;
@Test
public void mytest() {
myClient = new MyClient(environment, classCreator);
}
}
Upvotes: 1
Reputation: 87
My issue was that I was trying to mock an object which was final in my service. Just needed to remove final and it worked.
Upvotes: 1
Reputation: 13
Found this solution in comments that worked for me. Do this for all the mocks you are creating.
You need to instantiate the routingClientMock e.g.
routingClientMock = Mockito.mock(RoutingObjHtttpClient.class);
Upvotes: 0
Reputation: 131
I had the problem that I declared @Mock MyClass myClass and then tried to mock a behaviour inside my @BeforeAll annotated method:
@Mock
private MyClass myClass;
...
@BeforeAll
public void init()
{
...
Mockito.when(myClass.something()).thenReturn("Not found")
...
}
Apparently init() was executed before the mock initialization of myClass, so myClass == null inside init().
The solution was to change the annotation from @BeforeAll to @BeforeEach.
Upvotes: 3
Reputation: 99
Add @ExtendWith(MockitoExtension.class) annotation to the test class and it should work given You are using Junit 5+ version. If you are using older version of Junit use @RunWith(MockitoJUnitRunner.class) annotation.
Upvotes: 7
Reputation: 27
For me it worked when I added :
@RunWith(MockitoJUnitRunner.class).
@Before
public void createMocks() {
MockitoAnnotations.initMocks(this);
}
Upvotes: 2
Reputation: 8240
I had the same issue, but I found updating my tests (which were originally written for JUnit 3 and used the legacy setUp()
and tearDown()
methods) to JUnit 4 and modern annotated fixture methods worked.
Additionally, if you're using the Rule:
@Rule public MockitoRule rule = MockitoJUnit.rule();
Make sure the rule is also on a public class or you will receive the message:
How did getFields return a field we couldn't access?
Upvotes: 0
Reputation: 361
What solved this issue for me (combination of answers above and my own additions):
MockitoAnnotations.initMocks(this);
in the @Before
methodimport org.junit.Test;
instead of import org.junit.jupiter.api.Test;
When doing command + N --> Test...
in Intellij it generates (as a default at least) some boilerplate that did not work in my case.
Upvotes: 12
Reputation: 57
For me adding the annotation to the class:
@RunWith(MockitoJUnitRunner.class)
and modifying the version of Mockito solved this issue.
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.23.4</version>
<scope>test</scope>
</dependency>
Upvotes: 0
Reputation: 3941
For me, even after adding @RunWith(MockitoJUnitRunner.class)
it was not working.
Turned out, I had made the silly mistake of importing @Test
from
import org.junit.jupiter.api.Test;
instead of
import org.junit.Test;
After correcting it, it worked!
Upvotes: 7
Reputation: 24520
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 {
// ...
}
}
Upvotes: 73
Reputation: 1247
Same problem can occur if you are using Junit5 since there is no more '@RunWith' annotation.
In this case you should annotate your class with:
@ExtendWith(MockitoExtension.class)
public class MyTestClass {
...
You should also import into your dependency (Maven - pom.xml):
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
Upvotes: 94
Reputation: 1667
If you use junit.jupiter
with @ExtendWith(MockitoExtension.class)
for test class but mocks are null, ensure that @Test
annotation is imported from
import org.junit.jupiter.api.Test;
instead of org.junit.Test;
Upvotes: 44
Reputation: 11124
Try to to check if the method that you are calling is a final method or not.
Mockito cannot mock the final method. https://github.com/mockito/mockito/wiki/FAQ
Upvotes: 1
Reputation: 1269
It can also be an import problem, so make sure you have the appropriate imported package.
For example, the "org.easymock" package also does have an annotation called @Mock, which of course, won't work with Mockito specific setup.
Upvotes: 4
Reputation: 123
If you are also using PowerMock then
@RunWith(MockitoJUnitRunner.class)
can be replaced with
@RunWith(PowerMockRunner.class)
This will activate your @Mocks
and enable the PowerMock functionality.
Upvotes: -4
Reputation: 10511
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.
Upvotes: 147
Reputation: 91
@RunWith(SpringJUnit4ClassRunner.class)
at your classMockitoAnnotations.initMocks(this);
in @Before methodUpvotes: 4