【发布时间】:2015-12-18 18:59:14
【问题描述】:
我正在创建一个库,它将根据用户默认设置处理信息并将其保存在 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 测试
应用测试:
public class ApplicationTest extends ApplicationTestCase<SampleApplication> {
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测试的应用程序吗? 我在这里做错了吗?
【问题讨论】:
标签: android unit-testing android-testing