我坚持这个问题。
以下是我服务的代码:
public class APIService extends IntentService {
public static final String MY_SERVICE = "com.approject.myproject.APIService";
private HttpClient httpClient = null;
private ArrayList<AbbrRecord> results = null;
/*Constructor*/
public APIService(){
super("APIService");
}
/*Process created.*/
/*Intialize HttpClient.*/
@Override
public void onCreate(){
super.onCreate();
httpClient = new DefaultHttpClient();
}
/*HandleIntent from Activities*/
@Override
public void onHandleIntent(Intent i){
results = null;
HttpGet getMethod = new HttpGet("http://www.someurl.com" + i.getStringExtra("search_request")); //getData().toString()
try{
InputStream instream = null;
HttpResponse response = httpClient.execute(getMethod);
HttpEntity entity = response.getEntity();
if(entity != null){
instream = entity.getContent();
}
InputSource input_source = new InputSource(instream);
SAXParserFactory factory = SAXParserFactory.newInstance();
try{
SAXParser parser = factory.newSAXParser();
XMLReader xml_reader = parser.getXMLReader();
DataParser content_handler = new DataParser();
xml_reader.setContentHandler(content_handler);
xml_reader.setErrorHandler(content_handler);
xml_reader.parse(input_source);
results = content_handler.pullDataOut();
}catch(ParserConfigurationException e){
e.printStackTrace();
}catch(SAXException e){
e.printStackTrace();
}
} catch(ClientProtocolException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}
if(results == null){
if( i.getIntExtra("sender", -1) == 0){
Intent it = new Intent("DATA_WAS_NOT_RETRIEVED");
sendBroadcast(it);
Log.d(getClass().getSimpleName(), "DATA_WAS_NOT_RETRIEVED BROADCAST WAS SENT FOR MAIN");
} else {
Intent it = new Intent("DATA_WAS_NOT_RETRIEVED_RESULTS");
sendBroadcast(it);
}
} else {
if(i.getIntExtra("sender", -1) == 0){
Intent it = new Intent("RETRIEVE_DATA_MAIN");
it.putExtra("search_results", results);
sendBroadcast(it);
Log.d(getClass().getSimpleName(), "DATA_RETRIEVED BROADCAST WAS SENT FOR MAIN");
} else {
Intent it = new Intent("RETRIEVE_DATA_RESULTS");
it.putExtra("search_results", results);
sendBroadcast(it);
}
}
Log.d(getClass().getSimpleName(), "Intent was handled.");
}
@Override
public void onDestroy(){
super.onDestroy();
Log.d( getClass().getSimpleName(), "APIService is destroyed");
}
}
APIServiceTest.java
public class APIServiceTest extends ServiceTestCase<APIService>{
private BroadcastReceiver receiver1 = null;
private BroadcastReceiver receiver2 = null;
private ArrayList<AbbrRecord> results = null;
public APIServiceTest(){
super(APIService.class);
}
@Override
protected void setUp() throws Exception{
super.setUp();
receiver1 = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent){
ArrayList<AbbrRecord> results = (ArrayList)intent.getSerializableExtra("search_results");
}
};
receiver2 = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
ArrayList<AbbrRecord> results = (ArrayList)intent.getSerializableExtra("search_results");
}
};
IntentFilter filter1 = new IntentFilter();
filter1.addAction("RETRIEVE_DATA_MAIN");
getSystemContext().registerReceiver(receiver1, filter1);
IntentFilter filter2 = new IntentFilter();
filter2.addAction("DATA_WAS_NOT_RETRIEVED");
getSystemContext().registerReceiver(receiver2, filter2);
}
public void testMakeRequest() throws InterruptedException{
Intent i = new Intent();
i.putExtra("search_request", "asap");
i.putExtra("sender", 0);
i.setClassName("com.approject.myproject", "com.approject.myproject.APIService");
getSystemContext().startService(i);
assertFalse(results == null);
}
@Override
protected void tearDown() throws Exception{
super.tearDown();
if(receiver1 != null && receiver2 != null){
getSystemContext().unregisterReceiver(receiver1);
getSystemContext().unregisterReceiver(receiver2);
}
}
}
我已经尝试过使用MockApplication,MockContext类。
命令行输出: com.approject.APIServiceTest:
Failure in testMakeRequest:
junit.framework.AssertionFailedError
at com.approject.myproject.APIServiceTest.testMakeRequest(APIServiceTest.java:71)
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:430)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)
..
Test results for InstrumentationTestRunner=.F..
Time: 10.239
FAILURES!!!
Tests run: 3, Failures: 1, Errors: 0
日志:
02-07 19:54:13.358: DEBUG/AndroidRuntime(239): >>>>>>>>>>>>>> AndroidRuntime START<<<<<<<<<<<<<<
02-07 19:54:13.358: DEBUG/AndroidRuntime(239): CheckJNI is ON
02-07 19:54:13.568: DEBUG/AndroidRuntime(239): --- registering native functions ---
02-07 19:54:13.888: DEBUG/ddm-heap(239): Got feature list request
02-07 19:54:14.208: DEBUG/ActivityManager(59): Uninstalling process com.approject.myproject
02-07 19:54:14.238: INFO/ActivityManager(59): Start proc com.approject.myproject for added application com.approject.myproject: pid=245 uid=10029 gids={3003, 1015}
02-07 19:54:14.368: DEBUG/ddm-heap(245): Got feature list request
02-07 19:54:14.588: INFO/TestRunner(245): started: testMakeRequest(com.approject.myproject.APIServiceTest)
02-07 19:54:15.778: DEBUG/dalvikvm(245): GC freed 3273 objects / 241584 bytes in 83ms
02-07 19:54:17.398: DEBUG/APIService(245): DATA_RETRIEVED BROADCAST WAS SENT FOR MAIN
02-07 19:54:17.398: DEBUG/APIService(245): Intent was handled.
02-07 19:54:17.518: DEBUG/dalvikvm(245): GC freed 5615 objects / 327224 bytes in 62ms
02-07 19:54:17.888: DEBUG/APIService(245): APIService is destroyed
02-07 19:54:24.650: INFO/TestRunner(245): failed: testMakeRequest(com.approject.myproject.APIServiceTest)
02-07 19:54:24.650: INFO/TestRunner(245): ----- begin exception -----
02-07 19:54:24.679: INFO/TestRunner(245): junit.framework.AssertionFailedError
02-07 19:54:24.679: INFO/TestRunner(245): at junit.framework.Assert.fail(Assert.java:47)
02-07 19:54:24.679: INFO/TestRunner(245): at junit.framework.Assert.assertTrue(Assert.java:20)
02-07 19:54:24.679: INFO/TestRunner(245): at junit.framework.Assert.assertFalse(Assert.java:34)
02-07 19:54:24.679: INFO/TestRunner(245): at junit.framework.Assert.assertFalse(Assert.java:41)
02-07 19:54:24.679: INFO/TestRunner(245): at com.approject.myproject.APIServiceTest.testMakeRequest(APIServiceTest.java:71)...
02-07 19:54:24.689: INFO/TestRunner(245): ----- end exception -----
02-07 19:54:24.729: INFO/TestRunner(245): finished: testMakeRequest(com.approject.myproject.APIServiceTest)
02-07 19:54:24.749: INFO/TestRunner(245): started: testServiceTestCaseSetUpProperly(com.approject.myproject.APIServiceTest)
02-07 19:54:24.799: INFO/TestRunner(245): finished: testServiceTestCaseSetUpProperly(com.approject.myproject.APIServiceTest)
02-07 19:54:24.799: INFO/TestRunner(245): passed: testServiceTestCaseSetUpProperly(com.approject.myproject.APIServiceTest)
02-07 19:54:24.809: INFO/TestRunner(245): started: testAndroidTestCaseSetupProperly(com.approject.myproject.APIServiceTest)
02-07 19:54:24.829: INFO/TestRunner(245): finished: testAndroidTestCaseSetupProperly(com.approject.myproject.APIServiceTest)
02-07 19:54:24.829: INFO/TestRunner(245): passed: testAndroidTestCaseSetupProperly(com.approject.myproject.APIServiceTest)
02-07 19:54:24.859: DEBUG/AndroidRuntime(239): Shutting down VM
02-07 19:54:24.859: DEBUG/dalvikvm(239): DestroyJavaVM waiting for non-daemon threads to exit
02-07 19:54:24.869: DEBUG/ActivityManager(59): Uninstalling process com.approject.myproject
感谢您的时间。