Verify Method called throws Null Pointer exception.
See original GitHub issueMethod to Test from presenter
` override
fun syncLocalOrders(syncOrders: SyncOrders) {
if (syncOrders.orders.isNotEmpty()) {
view.showProgressDialog("Syncing Successful Call's")
repository.syncLocalOrder(syncOrders)
.subscribe({
var syncMessage = if (it.failure.count() > 0) {
"Call Synced Failed"
} else {
"Call synced successfully."
}
view.hideProgressDialog()
getUnsuccessfulCallData(false, syncMessage) //Null Pointer Exception
updateDatabase(it)
}, {
view.hideProgressDialog() // and enters here too after null pointer exception
view.showAlertDialog("Error", ErrorMessageFactory.createMessage(it))
})
} else {
getUnsuccessfulCallData(true, "")
}
}`
internal fun getUnsuccessfulCallData(showDialog: Boolean, syncMessage: String) {
repository.getUnsuccessfulCallData()
.subscribe({
if (it.noOrders.isNotEmpty()) {
syncUnsuccessfulCallData(it, syncMessage)
} else {
view.hideProgressDialog()
if (showDialog) {
view.showAlertDialog("", "no orders to sync")
} else {
if (syncMessage.isNotEmpty())
view.showSnackBar(syncMessage)
}
}
}, {
it.printStackTrace()
})
}
`
` @Test
fun syncLocalOrders_OrderNotEmptySuccessTest() {
val order = Order(
outletId = 1,
startDate = "Start Date",
endDate = "End Date",
syncLat = 9090.09009,
syncLng = 909009.990,
offRoute = 1,
notes = "notes",
remarks = "Remarks",
totalPromotionDiscount = 9090.998,
totalAmount = 90.90,
inventory = null,
promotionOrders = null
)
val listOfOrder = listOf(order, order)
val syncOrder = SyncOrders(listOfOrder)
val success = listOf(1, 2, 3, 4)
val failure = listOf<Int>()
val orderResponse = OrderResponse(success, failure)
whenever(repository.syncLocalOrder(syncOrder)).thenReturn(Observable.just(orderResponse))
presenter.syncLocalOrders(syncOrder)
verify(view).showProgressDialog(anyString())
verify(view).hideProgressDialog()
verify(presenter, times(1)).getUnsuccessfulCallData(eq(false), "Call synced successfully.")
verify(presenter, times(1)).updateDatabase(orderResponse)
}`
The main issue here is whenever I try to run the test syncLocalOrders_OrderNotEmptySuccessTest()
, the code enters to both subscribe and throwable of fun syncLocalOrders(syncOrders: SyncOrders)
(this was got by keeping breakpoints.) And what I knew was the code enters to throwable after getting Null Pointer Exception
on getUnsuccessfulCallData(false, syncMessage)
and the test fails Saying something like view.hideProgressDialog() wanted 1 time but was 2 times
. Can anybody tell me what am I doing wrong?
Issue Analytics
- State:
- Created 5 years ago
- Comments:12
Top Results From Across the Web
Mockito - NullpointerException when stubbing Method
The reason why is that any() returns a generic object reference. This object is unboxed to an integer, so the generic type is...
Read more >Mocking Exception Throwing using Mockito | Baeldung
Learn to configure a method call to throw an exception in Mockito. ... @Test(expected = NullPointerException.class) public void ...
Read more >How to Fix and Avoid NullPointerException in Java - Rollbar
NullPointerException in Java occurs when a variable is accessed which is not pointing to any object and refers to nothing or null.
Read more >Mockito 2.2.29 API - javadoc.io
Unstubbed methods often return null. If your code uses the object returned by an unstubbed call you get a NullPointerException.
Read more >Java NullPointerException - Detect, Fix, and Best Practices
1. NullPointerException when calling an instance method ... When we run the above program, it throws the following NullPointerException error ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Then try
verify(presenter).getUnsuccessfulCallData(eq(false), anyString())
There’s a limitation in Mockito-kotlin verifying non-null parameters - it’s mentioned in the wiki.
Your
repository
instance is mocked and thus will returnnull
for all unstubbed method calls. Since you didn’t stub theupdateUser
function it returned null and the NPE is thrown.