The problem is that ArgumentMatchers.any()
returns null
, which does not play well with Kotlin non-nullable types. If it works in your first snippet, then the sendFeedbackToEmail
argument must be nullable I guess (or it's a Java class). But the feedbackText
in the mockSendFeedbackToEmail
is not nullable, so Kotlin compiler inserts null-checks there. Thus the NullPointerException.
Try changing the method signature to:
fun FeedbackManager.mockSendFeedbackToEmail(feedbackText: String? = any()) {
...
}
If the sendFeedbackToEmail
argument is not nullable and you are using com.nhaarman.mockito_kotlin
, you can try the following:
fun FeedbackManager.mockSendFeedbackToEmail(feedbackText: String? = null) {
Mockito.`when`(this.sendFeedbackToEmail(feedbackText ?: any())).thenReturn("OK")
}
or just overload the function instead of using default value (to avoid a bit misleading calls like: feedbackManager.mockSendFeedbackToEmail(null)
):
fun FeedbackManager.mockSendFeedbackToEmail() {
Mockito.`when`(this.sendFeedbackToEmail(any())).thenReturn("OK")
}
fun FeedbackManager.mockSendFeedbackToEmail(feedbackText: String) {
Mockito.`when`(this.sendFeedbackToEmail(feedbackText)).thenReturn("OK")
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…