如何对startActivity进行单元测试?

How to unit test startActivity?

我是 android 测试的新手。请帮我写一个单元测试 class.

class SplashActivity : AppCompatActivity(){

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val intent = Intent(this, HomeActivity::class.java)
        startActivity(intent)
        finish()
    }
}

测试文件夹中的单元测试class

import androidx.lifecycle.Lifecycle
import androidx.test.core.app.ActivityScenario
import androidx.test.espresso.intent.Intents.getIntents
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.ext.truth.content.IntentSubject.assertThat
import com.codelife.dubizzle.ui.home.HomeActivity
import com.codelife.dubizzle.ui.home.SplashActivity
import org.junit.Test
import org.junit.runner.RunWith

/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */


@RunWith(AndroidJUnit4::class)
class ExampleUnitTest {
    @Test fun splashActivityTest() {

        val scenario = ActivityScenario.launch(SplashActivity::class.java).moveToState(Lifecycle.State.CREATED)
        assertThat(getIntents().first()).hasComponentClass(HomeActivity::class.java)



    }
}

错误:

java.lang.NullPointerException
    at androidx.test.espresso.intent.Intents.getIntents(Intents.java:258)
    at com.codelife.dubizzle.ExampleUnitTest.splashActivityTest(ExampleUnitTest.kt:37)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.robolectric.RobolectricTestRunner$HelperTestRunner.evaluate(RobolectricTestRunner.java:601)
    at org.robolectric.internal.SandboxTestRunner.evaluate(SandboxTestRunner.java:260)
    at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:130)
    at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:42)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.robolectric.internal.SandboxTestRunner.evaluate(SandboxTestRunner.java:84)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at androidx.test.ext.junit.runners.AndroidJUnit4.run(AndroidJUnit4.java:104)

您需要确保在每次测试之前初始化浓缩咖啡意图;这是通过声明这样的测试规则来完成的:

@get:Rule
val rule = IntentsTestRule(SplashActivity::class.java)

在那之后你的断言应该有效。您还可以使用 intended() 方法来验证意图,在这种特殊情况下,您可以这样做:

intended(hasComponent(HomeActivity::class.java.canonicalName))

你必须打电话给 Intents.init()

Intents.init()

val scenario = ActivityScenario.launch(SplashActivity::class.java)
scenario.moveToState(Lifecycle.State.CREATED)
scenario.onActivity {
    // assert...
}

Intents.release()