Restarting the application in the test on Android

I am creating a library that will process information in accordance with the user's default settings and save it to SharedPreferences, which can be changed by the developer in their application before initializing my library.

The SDK should only be initialized once per application instance, otherwise a RuntimeError will be fired. So on the application side in the Application class should be something like this:

public class SampleApplication extends Application { @Override public void onCreate() { super.onCreate(); //Here I can do something that will change the default configs //Here I initialize the SDK singleton method Sdk.initialize(); } } 

Abstract sdk implementation:

 public class Sdk { private static SampleApplication sInstance; private void Sdk(){ } public static SampleApplication getInstance() throws RuntimeException { if (sInstance == null) { throw new RuntimeException(); } return sInstance; } public static void initialize() { if (sInstance == null) { sInstance = new Sdk(); //save some information according to what is on the default configurations } else { throw new RuntimeException("Method was already initialized"); } } } 

The problem occurs when I want to test several scripts to call this method (which can only be called once per application instance).

So, I created an Android test that extends ApplicationTest

ApplicationTest:

  public class ApplicationTest extends ApplicationTestCase<SampleApplication> { public ApplicationTest() { super(SampleApplication.class); } } 

Test for Android:

 public class SampleTest extends ApplicationTest { @Override protected void setUp() throws Exception { // Here I restart the user preferences although I need to restart the application // terminateApplication(); // createApplication(); super.setUp(); } @Override protected void tearDown() throws Exception { super.tearDown(); } public void testDefaultSettings() { // Here is where I want to restart application input // some values on the user preferences settings in order // to test the output on sharedpreferences by the initialized method } } 

I tried to finish and create the application again, but without success. My question is: can I restart the Android test application? Am I something wrong here?

+6
source share
1 answer

I believe the problem you're really struggling with is the InstrumentationTestRunner problem: How to prevent the ActivityUnitTestCase from calling Application.onCreate? (and there seems to be no obvious fix for it)

those. onCreate() will still call onCreate() during its initialization, so after calling createApplication() your Sdk already initialized.

Regarding the question itself - I think the only option is to rethink the architecture of the Sdk class (or add some "reset" function, etc.)

 public class TestApplication extends Application { @Override public void onCreate() { super.onCreate(); // Sdk.terminate(); - If you specify TestApplication as an // application class in AndroidManifest, // you'll have to uncomment this(due to issue with test runner) Sdk.initialize(); } @Override public void onTerminate() { super.onTerminate(); Sdk.terminate(); } } 

Sdk class

 public class Sdk { private static Sdk sInstance; private void Sdk(){ } public static Sdk getInstance() throws RuntimeException { if (sInstance == null) { throw new RuntimeException(); } return sInstance; } public static void terminate() { sInstance = null; } public static void initialize() { if (sInstance == null) { sInstance = new Sdk(); //save some information according to what is on the default configurations } else { throw new RuntimeException("Method was already initialized"); } } } 

Tests:

 public class MyApplicationTest extends ApplicationTestCase<TestApplication> { public MyApplicationTest() { super(TestApplication.class); } public void testMultiplicationTests() { createApplication(); int answer = 42; assertEquals(42, answer); terminateApplication(); } public void testDefaultSettings() { createApplication(); assertNotNull(Sdk.getInstance()); terminateApplication(); } } 

NB! You can make this a little less offended if you use the special AndroidManifest for androidTest. Then you will not deal with the TestRunner problem when it calls the Create () function itself before the tests begin.

Hope this helps

+1
source

All Articles