Search code examples
javaandroidunit-testingjunitandroid-webview

Stopping Test's if JUnit Test fails


I'm trying to figure out How i can make my JUnits tests run a specific order as well as stop all test's if one of the test's fail. Here below i have included just some simple actionbar item test's. All i basically want to make sure is when i run my build/test via command line, i want the test to stop if one of these tests fails. Any Recommendations?

public void testPreconditions() {
    assertNotNull(instrumentation);
    assertNotNull(mWeb);
    assertNotNull(mActivity);
}

protected void setUp() throws Exception {
    super.setUp();

    instrumentation = getInstrumentation();
    mActivity = getActivity();
    setActivityInitialTouchMode(false);
    mWeb = (WebView) mActivity
            .findViewById(com.jaisonbrooks.webview.R.id.webview_main);
    mMockWebViewClient = new MockWebViewClient();
    mWeb.setWebViewClient(mMockWebViewClient);
    mSettings = mWeb.getSettings();
}

public void testThatButtonReloadWorks() {
    final View view = mActivity
            .findViewById(com.jaisonbrooks.webview.R.id.menu_refresh);
    mActivity.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            view.requestFocus();
            view.callOnClick();
        }
    });
}

public void testThatButtonForwardWorks() {
    final View view = mActivity
            .findViewById(com.jaisonbrooks.webview.R.id.menu_forward);
    mActivity.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            view.requestFocus();
            view.callOnClick();
        }
    });
}

public void testThatButtonBackWorks() {
    final View view = mActivity
            .findViewById(com.jaisonbrooks.webview.R.id.menu_back);
    mActivity.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            view.requestFocus();
            view.callOnClick();
        }
    });
}

Solution

  • If you're doing integration tests using JUnit, you could prefix the method name with _1, _2 etc.:

    public void _1_testThatButton()...
    public void _2_testSomethingSecond()...
    

    I suppose you could call System.exit() instead of throwing an exception, but if your tests are taking too long, that may be a bad sign for adding more tests in the future (adding more tests may discourage you from running them).

    If your're using ant, you can try this: How to make junit testing to stop after first failing test