Mockito - NullpointerException when stubbing Method

后端 未结 18 1550
半阙折子戏
半阙折子戏 2020-11-30 20:54

So I started writing tests for our Java-Spring-project.

What I use is JUnit and Mockito. It\'s said, that when I use the when()...thenReturn() option I can mock ser

相关标签:
18条回答
  • 2020-11-30 21:11

    Corner case:
    If you're using Scala and you try to create an any matcher on a value class, you'll get an unhelpful NPE.

    So given case class ValueClass(value: Int) extends AnyVal, what you want to do is ValueClass(anyInt) instead of any[ValueClass]

    when(mock.someMethod(ValueClass(anyInt))).thenAnswer {
       ...
       val v  = ValueClass(invocation.getArguments()(0).asInstanceOf[Int])
       ...
    }
    

    This other SO question is more specifically about that, but you'd miss it when you don't know the issue is with value classes.

    0 讨论(0)
  • 2020-11-30 21:18

    None of these answers worked for me. This answer doesn't solve OP's issue but since this post is the only one that shows up on googling this issue, I'm sharing my answer here.

    I came across this issue while writing unit tests for Android. The issue was that the activity that I was testing extended AppCompatActivity instead of Activity. To fix this, I was able to just replace AppCompatActivity with Activity since I didn't really need it. This might not be a viable solution for everyone, but hopefully knowing the root cause will help someone.

    0 讨论(0)
  • 2020-11-30 21:19

    Make sure you initialize your mocks.

    JUnit4 use @Before

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
    }
    

    JUnit5 use @BeforeEach

    @BeforeEach
    public void setup() {
        MockitoAnnotations.initMocks(this);
    }
    

    For JUnit5 check, you are using proper imports also.

    import org.junit.runner.RunWith
    import org.mockito.junit.MockitoJUnitRunner;
    
    @RunWith(MockitoJUnitRunner.class)
    
    0 讨论(0)
  • 2020-11-30 21:19

    In my case it was due to wrong import of the @Test annotation

    Make sure you are using the following import

    import org.junit.jupiter.api.Test;
    
    0 讨论(0)
  • 2020-11-30 21:26

    For future readers, another cause for NPE when using mocks is forgetting to initialize the mocks like so:

    @Mock
    SomeMock someMock;
    
    @InjectMocks
    SomeService someService;
    
    @Before
    public void setup(){
        MockitoAnnotations.initMocks(this); //without this you will get NPE
    }
    
    @Test
    public void someTest(){
        Mockito.when(someMock.someMethod()).thenReturn("some result");
       // ...
    }
    

    Also make sure you are using JUnit for all annotations. I once accidently created a test with @Test from testNG so the @Before didn't work with it (in testNG the annotation is @BeforeTest)

    0 讨论(0)
  • 2020-11-30 21:26

    Ed Webb's answer helped in my case. And instead, you can also try add

      @Rule public Mocks mocks = new Mocks(this);
    

    if you @RunWith(JUnit4.class).

    0 讨论(0)
提交回复
热议问题