0

I'm testing my app with Robolectric, but whenever there is an asynctask the execution flow continue without waiting for it, after some research I found this method flushBackgroundThreadScheduler() that execute all runnables that have been queued on the background scheduler, but I get the same results and onDone method is never called.

Am I using the right method? How to fix this?

EDIT

this my code setUp method + test method :

    @Before
    public void setUp() throws Exception {
        activity = Robolectric.setupActivity(SignUpActivity.class);
    }

    @Test
    public void startNewActivityWhenLoginSuccess() throws Exception {
        // Arrange
        String email = "aersdfsqdz@z.zz";
        String password = "zzzzzz";
        Class clazz = NameActivity.class;
        Intent expectedIntent = new Intent(activity,clazz);

        // Act
        activity.register_email.setText(email);
        activity.register_password.setText(password);
        activity.fragment1.button.callOnClick();
        Robolectric.flushBackgroundThreadScheduler(); //from 3.0
        //ShadowLooper.runUiThreadTasksIncludingDelayedTasks();

        // Assert
        ShadowActivity shadowActivity = Shadows.shadowOf(activity);
        Intent actualIntent = shadowActivity.getNextStartedActivity();
        assertTrue(expectedIntent.filterEquals(actualIntent));
    }
Abdennacer Lachiheb
  • 4,388
  • 7
  • 30
  • 61
  • 1
    Are you doing anything to mock the response of the logic happening within the AsyncTask? If you can provide the sample of your code that'd be incredibly helpful. – CodyEngel Jan 12 '17 at 18:05
  • @HuskyHuskie I updated my comment I'm just mocking my activity with Robolectric, I don't need to mock anything else, because Robolectric will handle everything inside that mocked activity right ? – Abdennacer Lachiheb Jan 13 '17 at 09:02
  • No Robolectric does a lot of magic but you still need to ensure you are telling it to treat AsyncTasks a certain way. Is `ShadowLooper` not working for you? – CodyEngel Jan 13 '17 at 14:42
  • So are you saying that I need to mock the AsyncTasks object ? the ShadowLooper works fine but I found that Robolectric.flushBackgroundThreadScheduler() is used since robolectric 3.0 that way I replaced ShadowLooper.runUiThreadTasksIncludingDelayedTasks(). – Abdennacer Lachiheb Jan 13 '17 at 14:48

0 Answers0