Android:Espresso:按意图进行的初始化活动-java.lang.RuntimeException:无法启动活动

时间:2019-04-16 10:32:04

标签: android android-espresso android-testing

我希望在开始活动之前通过意图将其初始化。所以我覆盖了方法getActivityIntent 这是Espresso的测试:

@RunWith(AndroidJUnit4::class)
class TraderDetailsActivityTest {
//val context = InstrumentationRegistry.getInstrumentation().getTargetContext()

   @Rule
   @JvmField
   var traiderDetailsActivityTestRule: ActivityTestRule<TraderDetailsActivity> = object : ActivityTestRule<TraderDetailsActivity>(TraderDetailsActivity::class.java) {
      override fun getActivityIntent(): Intent {
         val intent = Intent(InstrumentationRegistry.getInstrumentation().context, TraderDetailsActivity::class.java)
         val bundle = Bundle()
         val trader = Trader();
         bundle.putSerializable(TraderDetailsActivity.TRADER, trader)
         intent.putExtras(bundle)
         return intent
      }
   }

   @Test
   fun toolBar_height() {
      Espresso.onView(ViewMatchers.withId(R.id.toolBar))
                   .check(ViewAssertions.matches(CustomMatchers.withHeightResId(R.dimen.tool_bar_height)))
   }
}

但是开始测试时出现错误:

Started running tests

java.lang.RuntimeException: Could not launch activity
at androidx.test.runner.MonitoringInstrumentation.startActivitySync(MonitoringInstrumentation.java:473)
at androidx.test.rule.ActivityTestRule.launchActivity(ActivityTestRule.java:358)
at androidx.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:529)
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 androidx.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:104)
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 androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:389)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1879)
Caused by: java.lang.RuntimeException: Unable to resolve activity for: Intent { flg=0x14000000 cmp=com.myproject.test/md.dotfinance.tm.android.ui.activity.TraderDetailsActivity (has extras) }
at android.app.Instrumentation.startActivitySync(Instrumentation.java:385)
at androidx.test.runner.MonitoringInstrumentation.access$201(MonitoringInstrumentation.java:99)
at androidx.test.runner.MonitoringInstrumentation$4.call(MonitoringInstrumentation.java:449)
at androidx.test.runner.MonitoringInstrumentation$4.call(MonitoringInstrumentation.java:446)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)

Tests ran to completion.

1 个答案:

答案 0 :(得分:2)

尝试通过这种方式进行操作:

@get:Rule
var activityRule: ActivityTestRule<YourActivity> =
   ActivityTestRule(YourActivity::class.java, false, false)

@Before
fun setupTest() {
   val intent.putExtras = Intent()
   val bundle = Bundle()
   val trader = Trader();
   bundle.putSerializable(TraderDetailsActivity.TRADER, trader)
   intent.putExtras(bundle)
   activityRule.launchActivity(intent)
}