Skip to content

Annotations in espresso

Devrath edited this page May 17, 2021 · 10 revisions
Title
AndroidJUnit4ClassRunner
ActivityTestRule
The espresso formulae

Annotation - AndroidJUnit4ClassRunner

@RunWith(AndroidJUnit4ClassRunner::class)
class MainActivityTest {
    @Test
    fun greet() {
    }
}

AndroidJUnit4ClassRunner::class --> This specifies the tests written in this class is an android test


ActivityTestRule

@RunWith(AndroidJUnit4ClassRunner::class)
class MainActivityTest {

    @Rule @JvmField
    var activityRule = ActivityTestRule<MainActivity>(MainActivity::class.java)

    @Test
    fun greet() { }
}
  • ActivityTestRule is used to tell espresso which activity to run before running the tests in the test class.
  • By this rule the activity is run every time before each test.
  • @JvmField, this is required to make the @Rule work.

The espresso formulae

  • The espresso formulae consist of three parts ViewMatcher<--->ViewAction<--->ViewAssertion
onView(ViewMatcher)   // Locate a view on the screen. 
.perform(ViewAction)  // Perform a action on the screen like clicking, typing etc.
.check(Assertion)     // Also we can can check that whether it works as expected.
Clone this wiki locally