0

I am using SafetyNet in my application, following this guide: https://developer.android.com/training/safetynet/attestation, and I have code similar to this pattern (which is in provided link):

SafetyNet.getClient(this).attest(nonce, API_KEY)
    .addOnSuccessListener(this) {
        // ...
    }
    .addOnFailureListener(this) { e ->
        // ...
    }
}

Everything is working, I get success or failure when expected.

My question: is there a way I can unit test this code without calling attest() method on a real SafetyNetClient, but on mock instead? I can mock SafetyNetClient and SafetyNetApi.AttestationResponse using Mockito, but I don't know how to simulate success or failure events.

koto
  • 720
  • 2
  • 9
  • 29

1 Answers1

0
 @Mock private SafetyNetClient safetyNetClientMock;
 @Mock Context contextMock;
 @Mock Task SafetyNetApi.AttestationResponse task;

 @Before
  public void setUp() {
    when(SafetyNet.getClient(contextMock)).thenReturn(safetyNetClientMock);
    when(safetyNetClientMock.attest(any(byte[].class), anyString())).thenReturn(task);
    when(task.addOnSuccessListener(any())).thenReturn(task);
    when(task.addOnFailureListener(any())).thenReturn(task);
 }