Mockito - NullpointerException when stubbing Method

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

    I had the same problem and my issue was simply that I had not annotated the class properly using @RunWith. In your example, make sure that you have:

    @RunWith(MockitoJUnitRunner.class)
    public class Test {
    ...
    

    Once I did that, the NullPointerExceptions disappeared.

提交回复
热议问题