16

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.

vishal dharankar
  • 7,536
  • 7
  • 57
  • 93
  • A complete stacktrace would be greatly appreciated – Johnco Jul 23 '15 at 15:03
  • @Johnco i m getting nothing in logical just this exception on console – vishal dharankar Jul 24 '15 at 07:11
  • what`s the messages of the exception? FYI,[http://stackoverflow.com/questions/14839162/instrumentation-run-failed-due-to-java-lang-illegalaccesserror][1] [1]: http://stackoverflow.com/questions/14839162/instrumentation-run-failed-due-to-java-lang-illegalaccesserror – suiwenfeng Jul 27 '15 at 04:52
  • @TankSui the exception stack trace doesn't appear in logcat , its only message that i get in console , also links are pointing to different situation i have checked all of them – vishal dharankar Jul 28 '15 at 07:40

1 Answers1

1

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

}

eyalp55
  • 9
  • 4
  • Define well the try/catch block statement. – Jose Rodriguez Jul 27 '15 at 21:38
  • You need to decide on `throws` or `try` as your exeption checker. – eyalp55 Jul 27 '15 at 21:46
  • To solve your probleme you need an exeption handler and to implement it in the code. for more on the exeptions and their handling, I would recommend this https://docs.oracle.com/javase/tutorial/essential/exceptions/index.html – eyalp55 Jul 27 '15 at 21:52
  • @eyalp55 consider reading my comments on question please, i said i didn't get any details in exception stack , nothing to do with exception handling – vishal dharankar Oct 28 '15 at 14:35
  • I meant that you need to add an exeption handler or somthing of sorts to print out the thing that is causing the error message you recived. – eyalp55 Oct 29 '15 at 21:15