Mockito - NullpointerException when stubbing Method

后端 未结 18 1600
半阙折子戏
半阙折子戏 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条回答
  •  萌比男神i
    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.

提交回复
热议问题