我想检查在片段的onViewCreated点是否发生错误。
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import java.lang.IllegalStateException
class TestFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
throw IllegalStateException()
}
}
通过FragmentScenario启动上面的Fragment,并检查try-catch以查看是否得到我想要的错误。
@Test
fun errorTest() {
try {
launchFragmentInContainer<TestFragment>()
throw Exception("Error Not Catch!")
} catch (e: IllegalStateException) {
} catch (e: RuntimeException) {
}
}
但是,此代码未捕获错误,并留下了以下日志:
java.lang.IllegalStateException
at com.my.pkg.TestFragment.onViewCreated(TestFragment.kt:22)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:298)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1232)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1398)
at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1476)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1541)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2858)
at androidx.fragment.app.FragmentManager.dispatchPause(FragmentManager.java:2828)
at androidx.fragment.app.FragmentController.dispatchPause(FragmentController.java:280)
at androidx.fragment.app.FragmentActivity.onPause(FragmentActivity.java:419)
at android.app.Activity.performPause(Activity.java:7153)
at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1408)
at androidx.test.runner.MonitoringInstrumentation.callActivityOnPause(MonitoringInstrumentation.java:740)
at android.app.ActivityThread.performPauseActivityIfNeeded(ActivityThread.java:3901)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3878)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3852)
at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3826)
at android.app.ActivityThread.-wrap15(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1609)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal .os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal .os.ZygoteInit.main(ZygoteInit.java:807)
如果我在Test批注(@Test(expected = IllegalStateException :: class))中指定了预期的异常,则日志会有所不同,但是无论如何它还是会失败。
java.lang.AssertionError: Expected exception: java.lang.IllegalStateException
at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:32)
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.ext.junit.runners.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:392)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2075)
我无法以任何方式使测试成功。
答案 0 :(得分:0)
您可以用预期的异常注释测试,如果没有引发异常,则可以fail()
注释测试。
@Test(expected = NoActivityResumedException::class)
fun backPress_from_first_item_Exits() {
onView(withText(R.string.app_name)).check(matches(isDisplayed()))
// throws NoActivityResumedException
Espresso.pressBack()
// test should fail if the app is still open
fail()
}
在我的示例中,系统backPress将关闭应用程序并引发NoActivityResumedException。