3

I am writing unit test cases using Mockito and JUnit . But getting NullPointerException when running a test. On debugging I get to know that Mockito on method: when().thenReturn() is not returning a value for dependent methods and the calling program is calling those methods to get result.

Below is my dummy code to get idea of structure of code:

class B {
  public C getValue() {
    return C;
  }
}

class A {
  public D getAns(String q1, String q2) {
    return B.getValue().map(mapper::toD); //null pointer exception start here 
  } 
}

@RunWith(MockitoJunitrunner.test)
class TestA {
  
  @InjectMock
  A a;

  @Mock
  B b;
  C c;

  init() {
    when(b.getValue()).thenReturn(c);
  }

  @Test
  public void getA() {
    D ans=A.getAns(q1,q2);  //getting null pointer exception here 
    AssertNotNull(ans);
  }
}
3
  • Use @Mock to C c Commented Aug 26, 2020 at 5:57
  • There is nothing to inject into A and looks like getValue() is a static method. If that isn't the case please add code that represents your case. Commented Aug 26, 2020 at 5:58
  • getValue() is not static . and my code is exactly like above code Commented Aug 26, 2020 at 6:07

3 Answers 3

1

You have classes calling each others methods so it is better to use Mockito.RETURNS_DEEP_STUBS

In your Case:

A is calling B and B is calling C

Just replace:

 @InjectMock
  A a;

  @Mock
  B b;
  C c;

With :

A a = Mockito.mock(A.class, Mockito.RETURNS_DEEP_STUBS);
B b = Mockito.mock(B.class, Mockito.RETURNS_DEEP_STUBS);
C c = Mockito.mock(C.class, Mockito.RETURNS_DEEP_STUBS);
Sign up to request clarification or add additional context in comments.

2 Comments

A is calling B and B is calling C
@SakshiMishra then use A a = Mockito.mock(A.class, Mockito.RETURNS_DEEP_STUBS); B b = Mockito.mock(B.class, Mockito.RETURNS_DEEP_STUBS); instead of simple mock. Try it and let me know if you still get NPE ??
1

There may be multiple reason to why when(...).thenReturn(...) is not called :

  1. The data type which is used in when construct is not matching exact, for example, if you have a string and you pass null, its not same method call
  2. Ensure that the objects are getting initialized using the same approach. A spring injected resource is not same as the one created using new operator

1 Comment

This helped me track down my issue - with the combination of non-matching arguments and RETURNS_DEEP_STUBS, mockito was returning mocked stubs instead of what I was intending it to. Thanks!
0

use @InjectMock and @Mock to solve this issue

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.