Espresso intent test failing

前端 未结 4 771
日久生厌
日久生厌 2021-02-01 15:46

I\'m learning android instrumentation testing with espresso. I have an app which has a drawer menu and there is a menu called About. I was testing click on that menu item and co

4条回答
  •  情深已故
    2021-02-01 16:18

    Instead of using IntentsTestRule which is now @deprecated in java, you should use the recommended activityScenarioRule like so:

    @RunWith(AndroidJUnit4::class)
    @MediumTest
    class YourActivityTest {
    
        @get:Rule
            val activityScenario = activityScenarioRule()
    
        @Before
            fun setUp() {
                launchActivity()
                Intents.init()
            }
        
            @After
            fun tearDown() {
                Intents.release()
            }
    
        @Test
            fun should_goBackTo_MainActivity_onBackButton_click() {
    
                onView(withId(R.id.goBackBtn)).perform(click())
                intended(hasComponent(hasShortClassName(".MainActivity")))
            }
    }
    

    Don't forget adding this in your build.gradle file:

    android {
    defaultConfig {
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    testOptions {
    unitTests {
    includeAndroidResources = true
    }
    }
    // testing
    androidTestImplementation 'androidx.test:core:1.3.1-alpha02'
    androidTestImplementation 'androidx.test:core-ktx:'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'   
    //for the activityScenarioRule syntax to work in kotlin,
    //We add the ktx version of androidx.test.ext:junit
    androidTestImplementation 'androidx.test.ext:junit-ktx:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.3.0'
    androidTestImplementation 'androidx.test.espresso:espresso-intents:3.3.0'
    androidTestImplementation 'androidx.test:runner:1.3.0'
    androidTestImplementation 'androidx.test:rules:1.3.0'
    }
    

提交回复
热议问题