lundi 7 janvier 2019

Testing asynchronously request using Robolectric

I'm trying to test the response of an asynchronously request using robolectric.

This is my robolectric test class:

@RunWith(RobolectricTestRunner.class)
@Config(sdk = 18, application = Vision.class)

public class robolectricTest {

  Facade facade;
  private String username;
  private String password;
  ErrorListener errorListener = new ErrorListener();
  private TokenInfo response;


  @Before
  public void setup() throws Exception {
    username = "user1";
    password = "pass";
    facade = Vision.legacyComponent.provideFacade();
  }

  @Test
  public void authenticate() {
    System.out.println("outside");
    facade.authenticate(username, password, token -> {
      System.out.println("inside");
      assert token != null;
      return null;
    }, errorListener);
  }
}

The problem is (I guess), as it's an asynchron request, robolectric finishes the test, but the response has not come back yet.

So I can see in the console, the test prints outside but never the inside it doesn't matter if I add a Thread.sleep()

Is there a way I can let the runner wait for the response?

Thank you for every advice/response/help

Best regards

Aucun commentaire:

Enregistrer un commentaire