Mockito - NullpointerException when stubbing Method

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

    faced the same issue, the solution that worked for me:

    Instead of mocking the service interface, I used @InjectMocks to mock the service implementation:

    @InjectMocks
    private exampleServiceImpl exampleServiceMock;
    

    instead of :

    @Mock
    private exampleService exampleServiceMock;
    

提交回复
热议问题