2017-07-18 1 views
0

Ici, j'ai un échantillon de code dans le présentateur. Comment puis-je faire écrire un test pour onSuccess et onFailure dans l'appel de rénovationTest unitaire en rattrapage pour rappel

public void getNotifications(final List<HashMap<String,Object>> notifications){ 

     if (!"".equalsIgnoreCase(userDB.getValueFromSqlite("email",1))) { 
      UserNotifications userNotifications = 
        new UserNotifications(userDB.getValueFromSqlite("email",1),Integer.parseInt(userDB.getValueFromSqlite("userId",1).trim())); 
      Call call = apiInterface.getNotifications(userNotifications); 
      call.enqueue(new Callback() { 
       @Override 
       public void onResponse(Call call, Response response) { 
        UserNotifications userNotifications1 = (UserNotifications) response.body(); 


        if(userNotifications1.getNotifications().isEmpty()){ 
         view.setListToAdapter(notifications); 
         onFailure(call,new Throwable()); 
        } 
        else { 
         for (UserNotifications.Datum datum:userNotifications1.getNotifications()) { 
          HashMap<String,Object> singleNotification= new HashMap<>(); 
          singleNotification.put("notification",datum.getNotification()); 
          singleNotification.put("date",datum.getDate()); 
          notifications.add(singleNotification); 
         } 
         view.setListToAdapter(notifications); 
        } 
       } 

       @Override 
       public void onFailure(Call call, Throwable t) { 
        call.cancel(); 
       } 
      }); 
     } 
    } 

} 

Comment écrire UnitTesting pour couvrir tous les cas pour ce morceau de code.

Merci

Répondre

4

Lorsque vous voulez tester différentes réponses du service (API), il est probablement préférable de se moquer et retourner ce dont vous avez besoin.

@Test 
    public void testApiResponse() { 
     ApiInterface mockedApiInterface = Mockito.mock(ApiInterface.class); 
     Call<UserNotifications> mockedCall = Mockito.mock(Call.class); 

     Mockito.when(mockedApiInterface.getNotifications()).thenReturn(mockedCall); 

     Mockito.doAnswer(new Answer() { 
     @Override 
     public Void answer(InvocationOnMock invocation) throws Throwable { 
      Callback<UserNotifications> callback = invocation.getArgumentAt(0, Callback.class); 

      callback.onResponse(mockedCall, Response.success(new UserNotifications())); 
      // or callback.onResponse(mockedCall, Response.error(404. ...); 
      // or callback.onFailure(mockedCall, new IOException()); 

      return null; 
     } 
     }).when(mockedCall).enqueue(any(Callback.class)); 

     // inject mocked ApiInterface to your presenter 
     // and then mock view and verify calls (and eventually use ArgumentCaptor to access call parameters) 
    } 
+0

mec soo beaucoup Thankyou ... il a vraiment sauvé ma journée – Jay

+0

pourriez-vous me expliquer au sujet s'il vous plaît Mockito.doAnswer (quelque chose ..) – Jay

+0

Vous utilisez cette notation pour spécifier le comportement simulé. C'est similaire à Mockito.when (mockObject) .someMethod (any (Parameter.class)). ThenReturn (returnValue); mais celui-ci doit être utilisé pour les fonctions sans type de retour (void). Voir https://testing.googleblog.com/2014/03/whenhow-to-use-mockito-answer.html. –