Thursday, 19 July 2018

Verify a method that was called from another method

I must say, I'm relatively new with android tests and I have a problem which I don't know how to solve.
My idea is to verify that a method is called whenever I use another method which I keep failing with mockito (normally it shouldn't fail).
Keep in mind that I'm using Robolectric framework and just wanted to test with Mockito for verifying the number of invocations.
Imagine that I have an Object instanced :
        FirebaseManager firebaseManager = FirebaseManager_WithCacheSetTokenToNullAndSending
StatusSetToFalsePlusFirebaseCloudMessaging_ImplementedInApplicationButTokenNotRetrievedYet();

I know that mockito has a feature where you can : Verifying exact number of invocations / at least x / never
Now, my purpose is to verify that whenever i call the method setToken(token_from_google) where token_from_google is different of null, It will execute, inside, the other method saveState. However if the token is null, then the method saveState shouldn't be executed. I tried the following example by checking that post
//        FirebaseManager spy = spy(firebaseManager);
//        spy.setToken(token_from_google);
//        verify(spy, times(1)).saveState();

But it always keep failing and saying the following :
Wanted but not invoked:
firebaseManager.saveState();
-> at  com.android.FirebaseManager.saveState(FirebaseManager.java:141)

However, there was exactly 1 interaction with this mock:
firebaseManager.setToken("Random_token");
-> at com.android.FirebaseManagerTest.testSetToken(FirebaseManagerTest.java:599)


Wanted but not invoked:
firebaseManager.saveState();
-> at com.android.FirebaseManager.saveState(FirebaseManager.java:141)

However, there was exactly 1 interaction with this mock:
firebaseManager.setToken("Random_token");
-> at com.android.FirebaseManagerTest.testSetToken(FirebaseManagerTest.java:599)

Does anyone know which direction I should take in order to solve my situation ? Thank you


from Verify a method that was called from another method

No comments:

Post a Comment