I am running android Instrumentation tests to unit test activity and specifically if the WebView has loaded or not code is as follows, but each time i get exception
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'
There are no Logcat logs , just this message in console , tried on Genymotion as well as device both are on android 5.0.
Code is as follows
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);
}
}
I have tried searching but got no clue why this exception is occurring , please help.
To avoid the IllegalStateException in Java, it should be ensured that any method in code is not called at an illegal or inappropriate time. Calling the next() method moves the Iterator position to the next element.
An IllegalStateException is an unchecked exception in Java. This exception may arise in our java program mostly if we are dealing with the collection framework of java. util.
IllegalStateException is thrown when a method has been invoked either at illegal or inappropriate time. IllegalStateException indicates that our application is not in an appropriate state to perform requested operation. Another commonly reused exception is IllegalStateException.
Add a catch statment after the try stetment and give the try statment an exeption. the syntext might be a little off but that should fix your probleme
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With