Mockito - NullpointerException when stubbing Method

后端 未结 18 1548
半阙折子戏
半阙折子戏 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:00

    For me the reason I was getting NPE is that I was using Mockito.any() when mocking primitives. I found that by switching to using the correct variant from mockito gets rid of the errors.

    For example, to mock a function that takes a primitive long as parameter, instead of using any(), you should be more specific and replace that with any(Long.class) or Mockito.anyLong().

    Hope that helps someone.

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

    I had this issue and my problem was that I was calling my method with any() instead of anyInt(). So I had:

    doAnswer(...).with(myMockObject).thisFuncTakesAnInt(any())
    

    and I had to change it to:

    doAnswer(...).with(myMockObject).thisFuncTakesAnInt(anyInt())
    

    I have no idea why that produced a NullPointerException. Maybe this will help the next poor soul.

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

    None of the above answers helped me. I was struggling to understand why code works in Java but not in Kotlin.

    Then I figured it out from this thread.

    You have to make class and member functions open, otherwise NPE was being thrown.

    After making function open tests started to pass.

    You might as well consider using compiler's "all-open" plugin:

    Kotlin has classes and their members final by default, which makes it inconvenient to use frameworks and libraries such as Spring AOP that require classes to be open. The all-open compiler plugin adapts Kotlin to the requirements of those frameworks and makes classes annotated with a specific annotation and their members open without the explicit open keyword.

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

    As this is the closest I found to the issue I had, it's the first result that comes up and I didn't find an appropriate answer, I'll post the solution here for any future poor souls:

    any() doesn't work where mocked class method uses a primitive parameter.

     public Boolean getResult(String identifier, boolean switch)
    

    The above will produce the same exact issue as OP.

    Solution, just wrap it:

     public Boolean getResult(String identifier, Boolean switch)
    

    The latter solves the NPE.

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

    you need to initialize MockitoAnnotations.initMocks(this) method has to called to initialize annotated fields.

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

    for more details see Doc

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

    In my case, I missed add first

    PowerMockito.spy(ClassWhichNeedToBeStaticMocked.class);
    

    so this can be helpful to somebody who see such error

    java.lang.NullPointerException
        at org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.addAnswersForStubbing(PowerMockitoStubberImpl.java:67)
        at org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.when(PowerMockitoStubberImpl.java:42)
        at org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.when(PowerMockitoStubberImpl.java:112)
    
    0 讨论(0)
提交回复
热议问题