Wednesday, July 25, 2018

Verify a method that was called from another method

Leave a Comment

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

P.S.: As requested I'm showing the class implementation that I did :

package com.followapps.android;  import android.content.Context; import android.support.annotation.NonNull; import android.support.annotation.Nullable;  import com.followapps.android.internal.Configuration; import com.followapps.android.internal.service.RequestService; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GoogleApiAvailability; import com.google.firebase.FirebaseApp; import com.google.firebase.iid.FirebaseInstanceId;  /** * Class which manages the Firebase methods. */ public class FirebaseManager implements IFirebaseManager { private static final String TAG = FirebaseManager.class.getSimpleName();   private FirebaseManager(){}  /**  * Check if no new token was generated and if the token was sent and received with success to FA server.  */ @Override public boolean isTokenValidAndSent(FirebaseInstanceId firebaseInstanceId) {     final String tokenFromFirebase = firebaseInstanceId.getToken();     boolean isTokenSent = getSendingStatusTokenPreferences();     boolean isANewTokenAvailable = isTokenNew(tokenFromFirebase);      return isANewTokenAvailable || !isTokenSent; }  /**  * Fetch the token of Firebase.  * This method will compare the local token and the token retrieved from Firebase Instance.  */ @Nullable @Override public String getToken(FirebaseInstanceId firebaseInstanceId) {     String res = null;      final String tokenFromFirebase = firebaseInstanceId.getToken();     boolean isTokenSent = getSendingStatusTokenPreferences();     boolean isANewTokenAvailable = isTokenNew(tokenFromFirebase);      if (isANewTokenAvailable) {         saveTokenPreferences(tokenFromFirebase);         res = tokenFromFirebase;     } else if (!isTokenSent) {         res = getTokenPreferences();     }     return res; }  /**  * This method sends the token to FA servers.  * First, it will save to our local cache memory, then he will send the token to our {@link RequestService} class.  */ @Override public void sendToken(@Nullable String token) {     if(token != null) {         saveTokenPreferences(token);         saveSendingStatusTokenPreferences(false);         RequestService.requestPushNotificationToken(token);     } }  /**  * This method compare the token from our local cache memory and the token retrieved from Firebase instance.  *  * @return true if token is different or tokenFromCache equals to null but fetchedTokenFromFirebase has a value. false if tokens are equals or both tokens are nullable  */ private boolean isTokenNew(@Nullable final String fetchedTokenFromFirebase) {     String tokenFromCache = getTokenPreferences();     boolean res = false;      if (fetchedTokenFromFirebase != null) {         if (tokenFromCache != null) {             res = !tokenFromCache.equals(fetchedTokenFromFirebase);         } else {             res = true; //the case that we have a new token from firebase and nothing in cache => this is a new token         }     }     return res; }  /**  * This method saves the token to our local cache memory  */ private void saveTokenPreferences(@NonNull final String token) {     //Configuration     Configuration.savePushTokenNotification(token); }  /**  * This method retrieves the token from our local cache memory.  */ @Nullable public String getTokenPreferences() {     return Configuration.getPushTokenNotification(); }  /**  * This method saves the sending status of our token sent to FA servers.  */ @Override public void saveSendingStatusTokenPreferences(boolean tokenSent) {     Configuration.saveSendingStatusPushTokenNotification(tokenSent); }  /**  * This method gets the sending status of a token.  */ public boolean getSendingStatusTokenPreferences() {     return Configuration.getSendingStatusPushTokenNotification(); }  /**  * Class for building the FirebaseManager, it will check if the google play services is available and Firebase implemented.  */ public final static class Builder {      private int googlePlayServicesAvailable;     private boolean firebaseImplemented;      private Builder(){}      // for production     public Builder(Context mContext) {         this(GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(mContext), FirebaseApp.initializeApp(mContext)!=null);     }      // for test     public Builder(int isGooglePlayServicesAvailable, boolean firebaseImplemented) {         this.googlePlayServicesAvailable = isGooglePlayServicesAvailable;         this.firebaseImplemented = firebaseImplemented;     }      /**      * Check the status of Google Play Services in order to run Firebase      */     private boolean isGooglePlayServicesAvailable(){         boolean res = true;         if (this.googlePlayServicesAvailable != ConnectionResult.SUCCESS) {             res = false;         }         return res;     }      public boolean isFirebaseAndGooglePlayServicesWorking(){return this.isGooglePlayServicesAvailable() && this.firebaseImplemented; }       /**      * Create the FirebaseManager object      *      * @return Interface, but the object might be a null object in cases that firebase is not implemented and google play services not available or an object with full interaction with Firebase API.      * */     public IFirebaseManager build() {         return isFirebaseAndGooglePlayServicesWorking() ? new FirebaseManager() : new FirebaseManagerDeactivated();     }  } } 

0 Answers

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment