我正在创建一个库,它将根据用户默认设置处理信息,并将其保存在SharedPreferences上,开发人员可以在初始化我的库之前对其进行修改.
SDK应该只为每个应用程序实例初始化一次,否则会触发RuntimeError.所以在Application类的应用程序端应该是这样的:
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(); } }
sdk抽象实现:
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"); } } }
当我想测试几个方案来调用这个方法时(每个应用程序实例只能调用一次),就会出现问题.
所以我创建了一个扩展ApplicationTest的Android测试
ApplicationTest:
public class ApplicationTest extends ApplicationTestCase{ public ApplicationTest() { super(SampleApplication.class); } }
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 } }
我试图再次终止并创建应用程序,但没有成功.我的问题是有可能重新启动Android测试的应用程序吗?我在这里做错了吗?