【发布时间】:2019-06-10 09:01:37
【问题描述】:
我需要在所有测试之前只启动一次活动。
所以我为此使用注释@BeforeClass。
这里是 Espresso 的测试:
@RunWith(AndroidJUnit4::class)
class JsonViewActivityTest {
companion object {
init {
// things that may need to be setup before companion class member variables are instantiated
}
@get:Rule
var jsonViewIntentsTestRule = IntentsTestRule(JsonViewActivity::class.java, false, false)
private val instrumentation = InstrumentationRegistry.getInstrumentation()
private val context = instrumentation.getContext()
private val targetContext = instrumentation.getTargetContext()
private val intent = Intent(targetContext, JsonViewActivity::class.java)
@BeforeClass
@JvmStatic
fun beforAll() {
Intent(targetContext, JsonViewActivity::class.java)
val bundle = Bundle()
val inputStream = context.assets.open(StubUtil.TradersStub.ONE_TRADER_HAS_WALLETS.stubName)
val content = inputStream.readBytes().toString(StandardCharsets.UTF_8)
val jsonArray = GsonUtil.gson.fromJson(content, JsonArray::class.java)
val jsonTrader = jsonArray.get(0)
val trader = GsonUtil.gson.fromJson(jsonTrader, Trader::class.java)
val wallet = trader.wallets[0]
bundle.putString(JsonViewActivity.WALLET_JSON, GsonUtil.gson.toJson(wallet))
intent.putExtras(bundle)
jsonViewIntentsTestRule.launchActivity(intent)
}
@AfterClass
@JvmStatic
fun afterAll() {
jsonViewIntentsTestRule.finishActivity()
}
}
@Test
fun toolBar_height() {
onView(withId(R.id.toolBar))
.check(ViewAssertions.matches(CustomMatchers.withHeightResId(R.dimen.tool_bar_height)))
}
}
但测试 toolBar_height 失败并显示消息:
Client not ready yet..
Started running tests
java.lang.RuntimeException: No activities found. Did you forget to launch the activity by calling getActivity() or startActivitySync or similar?
at androidx.test.espresso.base.RootViewPicker.waitForAtLeastOneActivityToBeResumed(RootViewPicker.java:169)
at androidx.test.espresso.base.RootViewPicker.get(RootViewPicker.java:83)
at androidx.test.espresso.ViewInteractionModule.provideRootView(ViewInteractionModule.java:77)
at androidx.test.espresso.ViewInteractionModule_ProvideRootViewFactory.provideRootView(ViewInteractionModule_ProvideRootViewFactory.java:35)
at androidx.test.espresso.ViewInteractionModule_ProvideRootViewFactory.get(ViewInteractionModule_ProvideRootViewFactory.java:24)
at androidx.test.espresso.ViewInteractionModule_ProvideRootViewFactory.get(ViewInteractionModule_ProvideRootViewFactory.java:10)
at androidx.test.espresso.base.ViewFinderImpl.getView(ViewFinderImpl.java:62)
at androidx.test.espresso.ViewInteraction$2.call(ViewInteraction.java:276)
at androidx.test.espresso.ViewInteraction$2.call(ViewInteraction.java:268)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
【问题讨论】:
-
beforAll拼错了吗? -
我看到的正是你所看到的。我可以在运行任何测试之前启动一个活动,但是当第一个测试运行时,那个启动的活动就关闭了;我还可以看到活动视图窗口消失了。不涉及活动的测试可以正常工作,但发展活动的测试会失败。我能够针对同一个活动运行多个测试的唯一方法是添加 @Before 并为每个测试启动活动。当测试完成时,活动消失,我们再次启动它以进行下一次测试。这当然是我们试图避免的。
标签: android-espresso