question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Verify Method called throws Null Pointer exception.

See original GitHub issue

Method 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, "")
    }
}`

` @VisibleForTesting

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:closed
  • Created 5 years ago
  • Comments:12

github_iconTop GitHub Comments

1reaction
bohsencommented, Aug 13, 2018

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.

0reactions
nhaarmancommented, Sep 29, 2018

Your repository instance is mocked and thus will return null for all unstubbed method calls. Since you didn’t stub the updateUser function it returned null and the NPE is thrown.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found