我正在尝试为我的Android应用程序编写单元测试,我想模拟我的服务类.我想测试服务中的一些错误行为,例如连接错误或找不到文件.
为了简化我的问题,我开始了一个新的Android项目并创建了一个Activity和一个Service类:
MyAndroidProjectActivity.java
package br.org.venturus.android;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.TextView;
import br.org.venturus.android.service.MyService;
public class MyAndroidProjectActivity extends Activity {
private MyService service;
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder binder) {
service = ((MyService.MyBinder) binder).getService();
}
public void onServiceDisconnected(ComponentName className) {
service = null;
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); …Run Code Online (Sandbox Code Playgroud)