我是在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)
答案 0 :(得分:0)
您需要确保在每次测试之前已初始化意式浓缩咖啡的意图;这是通过声明如下测试规则来完成的:
@get:Rule
val rule = IntentsTestRule(SplashActivity::class.java)
之后,您的断言应该起作用。您还可以使用intended()
方法来验证意图,在这种情况下,您可以执行以下操作:
intended(hasComponent(HomeActivity::class.java.canonicalName))
答案 1 :(得分:0)
您必须致电Intents.init()
Intents.init()
val scenario = ActivityScenario.launch(SplashActivity::class.java)
scenario.moveToState(Lifecycle.State.CREATED)
scenario.onActivity {
// assert...
}
Intents.release()