gpt4 book ai didi

java - 使用 ActivityUnitTestCase 时出现 RuntimeException,但在 ActivityInstrumentationTestCase2 时不会

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:02:43 24 4
gpt4 key购买 nike

我正在尝试使用 ActivityUnitTestCase 测试 Android 应用程序的 MainActvity。出于某种原因,我什至无法开始测试,因为它失败并显示以下错误跟踪:

java.lang.RuntimeException: Exception during suite construction
at android.test.suitebuilder.TestSuiteBuilder$FailedToCreateTests.testSuiteConstructionFailed(TestSuiteBuilder.java:238)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:537)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1594)
Caused by: java.lang.NullPointerException: name == null
at java.lang.Class.getConstructorOrMethod(Class.java:446)
at java.lang.Class.getMethod(Class.java:915)
at android.test.suitebuilder.TestMethod.getAnnotation(TestMethod.java:60)
at android.test.suitebuilder.annotation.HasMethodAnnotation.apply(HasMethodAnnotation.java:39)
at android.test.suitebuilder.annotation.HasMethodAnnotation.apply(HasMethodAnnotation.java:30)
at com.android.internal.util.Predicates$OrPredicate.apply(Predicates.java:106)
at android.test.suitebuilder.annotation.HasAnnotation.apply(HasAnnotation.java:42)
at android.test.suitebuilder.annotation.HasAnnotation.apply(HasAnnotation.java:31)
at com.android.internal.util.Predicates$NotPredicate.apply(Predicates.java:122)
at android.test.suitebuilder.TestSuiteBuilder.satisfiesAllPredicates(TestSuiteBuilder.java:253)
at android.test.suitebuilder.TestSuiteBuilder.build(TestSuiteBuilder.java:189)
at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:371)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3943)
at android.app.ActivityThread.access$1300(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1189)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4447)
at java.lang.reflect.Method.invokeNative(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)

我无法调试它,因为它在到达我可以放在测试用例类中的任何断点之前就失败了。我对 ActivityInstrumentationTestCase2 类所做的任何测试都没有问题。

在控制台中显示以下错误消息:

Test run failed: Instrumentation run failed due to 'java.lang.RuntimeException'

我假设,它暗示无法成功调用 getInstrumentation()。如果是这样,我怎么才能创建一个有效的 Intent 来启动 MainActivity?

ActivityUnitTestCase 的代码:

public class MainActivityUnitTest extends 
android.test.ActivityUnitTestCase<MainActivity> {

private MainActivity activity;

public MainActivityUnitTest(String name) {
super(MainActivity.class);
}

@Override
protected void setUp() throws Exception {
super.setUp();
}

@SmallTest
public void testActivity() {

Intent intent = new Intent(getInstrumentation().getTargetContext(),
MainActivity.class);
startActivity(intent, null, null);
activity = getActivity();
assertNotNull(activity);
}
}

感谢您提供的任何帮助。

最佳答案

问题出在构造函数中的参数“name”中。删除它时,它运行得非常好。 :)

关于java - 使用 ActivityUnitTestCase 时出现 RuntimeException,但在 ActivityInstrumentationTestCase2 时不会,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18102887/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com