Android UI 测试无法解析 : Intent 的 Activity

标签 android testing junit kotlin android-espresso

我有一项 Activity 需要 Intent 才能开始。我正在尝试运行一个 UI 工具测试,该测试在开始 Activity 之前初始化 Intent ,但我的测试失败并出现以下 java.lang.RuntimeException。

我做错了什么?我找到了一个例子 here但我无法发现任何差异...

谢谢!

java.lang.RuntimeException: Could not launch activity
at android.support.test.runner.MonitoringInstrumentation.startActivitySync(MonitoringInstrumentation.java:460)
at android.support.test.rule.ActivityTestRule.launchActivity(ActivityTestRule.java:354)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:525)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at android.support.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:101)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:384)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2074)
Caused by: java.lang.RuntimeException: Unable to resolve activity for: Intent { flg=0x14000000 cmp=com.afitzwa.viewmodeltestingapp.test/com.afitzwa.viewmodeltestingapp.MainActivity (has extras) }
at android.app.Instrumentation.startActivitySync(Instrumentation.java:439)
at android.support.test.runner.MonitoringInstrumentation.access$101(MonitoringInstrumentation.java:96)
at android.support.test.runner.MonitoringInstrumentation$4.call(MonitoringInstrumentation.java:436)
at android.support.test.runner.MonitoringInstrumentation$4.call(MonitoringInstrumentation.java:433)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)

ExampleInstrumentedTest.kt

package com.afitzwa.viewmodeltestingapp

import android.content.Intent
import android.support.test.InstrumentationRegistry
import android.support.test.rule.ActivityTestRule
import android.support.test.runner.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*
import org.junit.Rule

/**
 * Instrumented test, which will execute on an Android device.
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {

    @Rule
    @JvmField
    val activityRule = object : ActivityTestRule<MainActivity>(MainActivity::class.java) {
        override fun getActivityIntent(): Intent {
            return Intent(InstrumentationRegistry.getInstrumentation().context, MainActivity::class.java)
                .apply {
                    putExtra("TEST", "TEST")
                }
        }
    }

    @Test
    fun useAppContext() {
        // Context of the app under test.
        val appContext = InstrumentationRegistry.getTargetContext()
        assertEquals("com.afitzwa.viewmodeltestingapp", appContext.packageName)
    }
}

主要 Activity .kt

package com.afitzwa.viewmodeltestingapp

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import com.afitzwa.viewmodeltestingapp.ui.main.MainFragment

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_activity)
        if (savedInstanceState == null) {
            supportFragmentManager.beginTransaction()
                .replace(R.id.container, MainFragment.newInstance())
                .commitNow()
        }
        val data = intent.getStringExtra(TEST_EXTRA)
    }

    companion object {
        val TEST_EXTRA = "foo"
    }
}

build.gradle(应用程序)

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.afitzwa.viewmodeltestingapp"
        minSdkVersion 23
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'android.arch.lifecycle:extensions:1.1.1'
    testImplementation 'junit:junit:4.12'

    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test:rules:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

最佳答案

您链接到的示例使用了 InstrumentationRegistry.getInstrumentation().targetContext,您使用了 InstrumentationRegistry.getInstrumentation().context

这些不等同的原因是因为 android 不允许您直接从外部上下文启动 Activity 。 targetContext 属性配置为应用程序的一部分,不会触发这些约束,但 Instrumentation 的 context 不被视为应用程序的一部分,因此无法解析 Activity 。

关于Android UI 测试无法解析 : Intent 的 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54381428/

相关文章:

android - 使用 Jetpack Compose 的深层链接导航到可组合项

java - 将 spring 配置文件添加到 SpringBootTest 中的现有配置文件中

Python测试整个脚本

java - 对抽象类的扩展进行强制测试

java - IntelliJ 运行/调试配置中的变量

Android Unity - 更改启动器 Activity

android - 带有下拉图标的微调器背景颜色

php - Laravel 获取随机数的静态数组并将其用于分页

java - POI 在从 excel 读取数字数据时追加 .0

android - ListView 中的标题 View 高度