Home » Android » android – Calling a method of the tested Activity from a test using espresso and see its results

android – Calling a method of the tested Activity from a test using espresso and see its results

Posted by: admin June 16, 2020 Leave a comment

Questions:

I’m creating and Android Application. I’m using Espresso for testing.

I’ve an Activity with the method:

public void render(Recipe recipe){
    //draw the recipe to the activity
}

I want to test that this method works correctly.

Not working solution 1

I’ve tested the following

@Test
public void viewPaintsRecipes() {
    final Activity activity = activityRule.launchActivity(new Intent());
    ((MainActivity)activity).render(Arrays.asList(new Recipe[]{recipe}));
    onView(withId(R.id.text)).check(matches(withText(recipe.toString())));
}

I get an Exception.

Only the original thread that created a view hierarchy can touch its
views.

Not working solution 2

I’ve also tried to put the two lines inside a runnable runned by a Handler in the main thread, but the test hangs.

How can I acchieve this?

Notes

I attach the full test. Note that I use dagger and Mockito too.

@RunWith(AndroidJUnit4.class)
@LargeTest
public class MainActivityTestWithMockPresenter {
    Recipe recipe = new Recipe("sampleTitle");
    @Rule
    public ActivityTestRule<MainActivity> activityRule = new ActivityTestRule(MainActivity.class, true, false);

    @Mock
    MainActivityPresenter mockPresenter;
    @Mock
    AndroidApplication mockContext;

    @Before
    public void insertMockedComponent(){
        MockitoAnnotations.initMocks(this);
        Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
        AndroidApplication app = (AndroidApplication) instrumentation.getTargetContext().getApplicationContext();
        MyModule mockedMyModule = new MyModule(mockContext){
            @Provides
            public MainActivityPresenter getMainActivityPresenter(){
                return mockPresenter;
            }
        };
        MyComponent component = DaggerMyComponent.builder().myModule(mockedMyModule).build();
        app.setComponent(component);
    }

    @Test
    public void viewPaintsRecipes() {
        final Activity activity = activityRule.launchActivity(new Intent());
        ((MainActivity)activity).render(Arrays.asList(new Recipe[]{recipe}));
        onView(withId(R.id.text)).check(matches(withText(recipe.toString())));
    }
}

Thanks

How to&Answers:

I’ve managed to get this to work with these dependencies in my build.gradle file:

androidTestCompile 'com.android.support:support-annotations:23.1.0'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'
androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'

You need to use the ActivityTestRule.runOnUiThread(@NotNull java.lang.Runnable runnable) method as follows:

@Test
public void testRecipeRender() throws Throwable {
    // When.
    activityTestRule.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            activityTestRule.getActivity().render(someRecipe);
        }
    });

    // Then.
    // assert stuff about the recipe rendering here
}