Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android TestRunner fails due to IllegalState exception

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.

like image 888
vishal dharankar Avatar asked Jul 17 '15 12:07

vishal dharankar


People also ask

How do I resolve Java Lang IllegalStateException?

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.

Is IllegalStateException a checked exception?

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.

When should you throw IllegalStateException?

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.


1 Answers

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);
}

}

like image 139
eyalp55 Avatar answered Oct 08 '22 11:10

eyalp55