Is it possible to run Android Espresso unit tests in @BeforeClass annotated methods?

I had the same issue and it was only because of the Rule, you can set the activity to launch in rule's constructor:

@Rule
public ActivityTestRule<MainActivity> menuActivityTestRule = 
        new ActivityTestRule<>(MainActivity.class, true, true);

last argument is responsible for launching the activity.


Hate to see this question unanswered.

So, for everyone who might be stumbling upon this:

My solution was to use the @FixMethodOrder(MethodSorters.NAME_ASCENDING) annotation on the test class and renaming the first test case to aaa_my_testcase.

See: MethodSorters, FixMethodOrder.


I was having the same issue because i was testing standalone fragments not an activity, I have created FragmentTestRule extending ActivityTestRule. And i have to call launchActivity() method in every test.

@Test
 public void recyclerViewItemClickTest() {

        mFragmentTestRule.launchActivity(null);
}