yat*_*ade 6 android android-service android-testing
我一直在尝试IntentService使用Android提供的ServiceTestCase类来测试我,但测试失败了testServiceTestCaseSetUpProperly,首先失败了.我的代码如下:
public class MyClientServiceTest
extends ServiceTestCase<MyClientService> {
public static final String TAG = MyClientServiceTest.class.getName();
public static final String FILE_PREFIX = "test_";
private boolean bLoggingIn = false;
private boolean bLoggingOut = false;
private boolean bSendingScanRequest = false;
private boolean bGettingTemplates = false;
private final Class SERVICE_CLASS = MyClientService.class;
private RenamingDelegatingContext rdContext = null;
public MyClientServiceTest() {
super(MyClientService.class);
}
public MyClientServiceTest(Class<MyClientService> serviceType) {
super(MyClientService.class);
}
@Override
public void setUp() throws Exception {
super.setUp();
rdContext = new RenamingDelegatingContext
(getSystemContext(), FILE_PREFIX);
setContext(rdContext);
}
public void testThrowsExceptionOnNoActionPassedInIntent() {
Intent intent = new Intent(rdContext, SERVICE_CLASS);
Exception e = null;
try{
startService(intent);
} catch(IllegalArgumentException ex) {
e = ex;
} finally {
assertNotNull(e);
}
}
public void testThrowsExceptionOnInvalidAction() {
Intent intent = new Intent(rdContext, SERVICE_CLASS);
intent.setAction("SurelyInvalidAction");
Exception e = null;
try {
startService(intent);
} catch(IllegalArgumentException ex) {
e = ex;
} finally {
assertNotNull(e);
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用JUnit运行此测试时会发生什么是:
org.itay.mobile.test.MyClientServiceTest
testServiceTestCaseSetUpProperly(org.itay.mobile.test.MyClientServiceTest)
junit.framework.AssertionFailedError
at android.test.ServiceTestCase.setupService(ServiceTestCase.java:152)
at android.test.ServiceTestCase.testServiceTestCaseSetUpProperly(ServiceTestCase.java:330)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448)
testThrowsExceptionOnInvalidAction(org.itay.mobile.test.MyClientServiceTest)
junit.framework.AssertionFailedError
at org.itay.mobile.test.MyClientServiceTest.testThrowsExceptionOnInvalidAction(MyClientServiceTest.java:61)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448)
testThrowsExceptionOnNoActionPassedInIntent(org.itay.mobile.test.MyClientServiceTest)
junit.framework.AssertionFailedError
at org.itay.mobile.test.MyClientServiceTest.testThrowsExceptionOnNoActionPassedInIntent(MyClientServiceTest.java:48)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448)
Run Code Online (Sandbox Code Playgroud)
即使我已经收集了测试Android IntentServices 特别令人生畏的testServiceTestCaseSetUpProperly测试,但测试失败了,这意味着(根据Android文档)我的服务无法正确初始化Context.我怎么做?如果不是这种情况(因为我做初始化使用上下文setContext()中setUp()),可以做些什么来解决此问题?
此外,我非常感谢有人指点我提供Android服务测试的一般指导(当然还有示例实现).
| 归档时间: |
|
| 查看次数: |
2077 次 |
| 最近记录: |