gpt4 book ai didi

java - 由于 IllegalState 异常,Android TestRunner 失败

转载 作者:IT老高 更新时间:2023-10-28 23:36:12 28 4
gpt4 key购买 nike

我正在运行 android Instrumentation 测试以对 Activity 进行单元测试,特别是 WebView 是否已加载代码如下,但每次我得到异常

Running tests Test running started Test failed to run to completion. Reason: 'Instrumentation run failed due to 'java.lang.IllegalStateException''. Check device logcat for details Test running failed: Instrumentation run failed due to 'java.lang.IllegalStateException'

没有 Logcat 日志,只有控制台中的这条消息,在 Genymotion 上尝试过,设备都在 android 5.0 上。

代码如下

public class WebViewActivityTest extends ActivityInstrumentationTestCase2 <WebViewActivity> {

WebView webView;
WebViewActivity testActivity;

public WebViewActivityTest()
{
super(WebViewActivity.class);
}

@Override
protected void setUp() throws Exception {
super.setUp();
testActivity = getActivity();

}

public void testWebView()
{
webView = (WebView)testActivity.findViewById(R.id.webView);
assertNotNull(webView);
}

public void testPreconditions() {

assertNotNull("Webview activity is null",testActivity);
}
}

我已尝试搜索,但不知道为什么会发生此异常,请帮助。

最佳答案

在 try 语句之后添加一个 catch 语句,并给 try 语句一个异常(exception)。语法可能有点不对,但应该可以解决您的问题

public class WebViewActivityTest extends ActivityInstrumentationTestCase2 <WebViewActivity> {

WebView webView;
WebViewActivity testActivity;

public WebViewActivityTest()
{
super(WebViewActivity.class);
}

@Override
protected void setUp() throws(exeption e) {
super.setUp();
testActivity = getActivity();
catch(exeption e)
System.out.println("Nope!);

}

public void testWebView()
{
webView = (WebView)testActivity.findViewById(R.id.webView);
assertNotNull(webView);
}

public void testPreconditions() {

assertNotNull("Webview activity is null",testActivity);
}

}

关于java - 由于 IllegalState 异常,Android TestRunner 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31475993/

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