1

我是android测试的新手。请帮我为这门课写一个单元测试。

class SplashActivity : AppCompatActivity(){

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

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

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$1.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$1.evaluate(RobolectricTestRunner.java:601)
    at org.robolectric.internal.SandboxTestRunner$2.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$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.robolectric.internal.SandboxTestRunner$1.evaluate(SandboxTestRunner.java:84)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at androidx.test.ext.junit.runners.AndroidJUnit4.run(AndroidJUnit4.java:104)
4

2 回答 2

1

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

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

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

intended(hasComponent(HomeActivity::class.java.canonicalName))
于 2019-01-16T11:26:22.460 回答
0

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

Intents.init()

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

Intents.release()
于 2019-09-17T16:02:46.050 回答