ActivityScenario "close()" doesn't work as expected

未關閉
#2,111 3 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
3/5
預估耗時
1-2 天
新手友好度
35/100
Issue 類型
缺陷
描述清晰度
需要釐清
活躍度
停滯
技術堆疊
java

研究方向

先從 ActivityScenario.close() 以及堆疊追蹤位置 ActivityScenario.java:416、:449 和 :684 開始,然後使用 ActivityScenario.launch 和 @After 重現 instrumented test。當 close() 在沒有所報告的 AssertionError 的情況下達到 DESTROYED 時,即視為完成;該 issue 未提供公開 repository 或指定名稱的測試檔案。

由索引模型根據 Issue 內容生成。

描述

Description

I'm running an instrumented test using ActivityScenario, so I launch the test, and after the test finishes, I call the method close() of the ActivityScenario.

Then, I received the following error:

java.lang.AssertionError: Activity never becomes requested state "[DESTROYED]" (last
                  lifecycle transition = "RESUMED")
                  	at androidx.test.core.app.ActivityScenario.waitForActivityToBecomeAnyOf(ActivityScenario.java:449)
                  	at androidx.test.core.app.ActivityScenario.moveToState(ActivityScenario.java:684)
                  	at androidx.test.core.app.ActivityScenario.close(ActivityScenario.java:416)
                  	at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:59)
                  	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
                  	at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
                  	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
                  	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
                  	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
                  	at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
                  	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
                  	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
                  	at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
                  	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
                  	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
                  	at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
                  	at org.junit.runners.Suite.runChild(Suite.java:128)
                  	at org.junit.runners.Suite.runChild(Suite.java:27)
                  	at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
                  	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
                  	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
                  	at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
                  	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
                  	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
                  	at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
                  	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:67)
                  	at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:58)
                  	at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:446)
                  	at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2218)

I don't know exactly why, but every time that I move the scenario to state DESTROYED the test crashes with the exception above.
When I remove the method "close" calling, the test starts to work as expected.

Steps to Reproduce
  • Create an instrumented test for an Activity
  • Starts the test using the method "ActivityScenario.launch"
  • Closes the scenario using an "@After" JUnit annotation
  • The crash will happen

Some libraries used:

  • Mockk
  • An abstraction of MockWebServer
Expected Results
  • Test pass without problems
Actual Results
  • Test crashing when calling close method.
AndroidX Test and Android OS Versions

MacBook Pro Apple M1 Pro
OS: Ventura 13.5
JDK version: java 17.0.8
Gradle version: 8.5
AGP: 8.2.1
AndroidX core library version: 1.5.0
Emulator: Pixel 4 API 33

Link to a public git repo demonstrating the problem:
主要語言
Java
星號
1.2k
分支
343
平均合併
1 天 18 小時
30 天內合併 PR
16

環境準備

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

android/android-test 的其他 Issue

查看 android/android-test 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。