Skip to content

Commit 88187cd

Browse files
committed
Improve error message on NPE in on
1 parent 9cd2095 commit 88187cd

File tree

4 files changed

+1050
-1
lines changed

4 files changed

+1050
-1
lines changed

mockito-kotlin/src/main/kotlin/com/nhaarman/mockitokotlin2/Mockito.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ class KStubbing<out T>(private val mock: T) {
215215
return try {
216216
Mockito.`when`(mock.methodCall())
217217
} catch (e: NullPointerException) {
218-
throw MockitoKotlinException("NullPointerException thrown when stubbing. If you are trying to stub a generic method, try `onGeneric` instead.", e)
218+
throw MockitoKotlinException("NullPointerException thrown when stubbing.\nThis may be due to two reasons:\n\t- The method you're trying to stub threw an NPE: look at the stack trace below;\n\t- You're trying to stub a generic method: try `onGeneric` instead.", e)
219219
}
220220
}
221221
}

mockito-kotlin/src/test/kotlin/test/Classes.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ open class Open {
3636
}
3737

3838
open fun stringResult() = "Default"
39+
40+
fun throwsNPE(): Any = throw NullPointerException("Test")
3941
}
4042

4143
class Closed

mockito-kotlin/src/test/kotlin/test/MockitoTest.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,17 @@ class MockitoTest : TestBase() {
957957
expect(mock.stringResult()).toBe("b")
958958
}
959959

960+
@Test
961+
fun doReturn_throwsNPE() {
962+
expectErrorWithMessage("look at the stack trace below") on {
963+
964+
/* When */
965+
mock<Open> {
966+
on { throwsNPE() } doReturn "result"
967+
}
968+
}
969+
}
970+
960971
@Test
961972
fun doReturn_withGenericIntReturnType_on() {
962973
/* Expect */

0 commit comments

Comments
 (0)