0

I have a mockito junit's test using the command below. The variable serviceTask is an Interface's instancie called ServiceTask. I'm using a @Mock in the declarection

Declaraction:

@Mock 
private ServiceTask

Command Line:

Mockito.when(serviceTask.getTask(Mockito.anyLong())).thenReturn(new Task());

Stack Trace:

java.lang.NullPointerException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:90) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55) at java.lang.reflect.Method.invoke(Method.java:508) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.mockito.internal.runners.DefaultInternalRunner$1$1.evaluate(DefaultInternalRunner.java:54) at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:239) at org.junit.rules.RunRules.evaluate(RunRules.java:20) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:99) at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:105) at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:40) at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)

5
  • 1
    Welcome to Stack Overflow. Please take the tour to learn how Stack Overflow works and read How to Ask on how to improve the quality of your question. Then edit your question to include your source code as a working minimal reproducible example, which can be compiled and tested by others. Commented Apr 27, 2022 at 16:59
  • 1
    I often do it this way: Mockito.doReturn(new Task()).when(serviceTask).getTask(org.mockito.ArgumentMatchers.anyLong())); Commented Apr 27, 2022 at 17:03
  • I tried it and the nullPointer persist. I saw there is a check inside the method serviceTask.getTask() that it tests an atributted the class Task. Then the result is null to the new Task(), this ckeck failed. How could I resolve this? Commented Apr 27, 2022 at 17:14
  • 1
    @LucianaOliveira do you use MockitoJunitRunner to run the test? Commented Apr 28, 2022 at 0:25
  • Yes, I do. @HariHaravelan, I use this command line @RunWith(MockitoJUnitRunner.class) before the public class. It's so difficult to understand it. Commented Apr 28, 2022 at 11:59

1 Answer 1

1

The only reason for NPE if it happens at the line of code you mentioned above is serviceTask is null.

If you're not using the correct test runner which can help you to initialise the mock instance, then you can actually initialise them manually in your setUp method by using MockitoAnnotations.initMocks(this);

But if you already did so or used the correct test runner, then probably you linked to a wrong line of code. Try to debug yourself again.

Your stack trace doesn't look like it caused from your linked line of code.

Sign up to request clarification or add additional context in comments.

2 Comments

The question with only one line for declaration and one line for the test code, means you assume that you are correct for everything else, this could be a wrong assumption.
Yes, you are rigth. The problem is not in the Mockito's syntax. I found an object that it is null. So the NPE is the Java exception, not the mockito's syntax. Thanks a lot to spend your time in this simple question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.