0

I have simple Application class:

public class MainApplication extends Application {
    public MainApplication() throws Exception {
        throw new Exception();
    }
}

and simple test:

public class SimpleTests extends AndroidTestCase {
    public void testSum() {
        assertEquals(4, 2 + 2);
    }
}

why I get an Exception on my tests starting? How my test is related with the Application class?

Running tests
Test running startedTest running failed: Instrumentation run failed due to 'java.lang.Exception'
Empty test suite.

Android Studio v.1.5.1

UPDATE: my Application subclass of course is more complex and contains business logic in method "onCreate". I do not want to run this logic when I run my tests.

3
  • 2
    "why I get an Exception on my tests starting?" -- assuming that you have this Application subclass registered in your manifest (android:name on the <application> element), an instance of that Application subclass will be created every time your app's process starts. "How my test is related with the Application class?" -- apparently, you are testing an Android app that is using this Application class as described in my previous paragraph. If you feel that this is not the case, perhaps you might post the relevant manifest and test code. Commented Dec 11, 2015 at 23:21
  • Thanks. How I prevent running an Application subclass only in my tests? And see update, please. Commented Dec 12, 2015 at 8:28
  • "How I prevent running an Application subclass only in my tests?" -- you don't. You fix your Application subclass so that it will work when being tested. Commented Dec 12, 2015 at 11:50

2 Answers 2

1

An option is override Application class for tests.

  1. Use custom testInstrumentationRunner in gradle configuration:

    testInstrumentationRunner 'com.xxx.test.CustomTestRunner'
    
  2. Implement Runner that doesn't use your Application class:

    public class CustomTestRunner extends InstrumentationTestRunner {
        @Override
        public Application newApplication(ClassLoader cl, String className, Context context) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
            return super.newApplication(cl, Application.class.getName(), context);
        }
    }
    
Sign up to request clarification or add additional context in comments.

Comments

0

It's because you are throwing it by code in constructer of Application

Remove

throw new Exception();

From MainApplication constructer

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.