sách gpt4 ăn đã đi

android - 在我的测试用例中启动服务时未调用 onStartCommand()

In lại 作者:太空狗 更新时间:2023-10-29 14:18:57 25 4
mua khóa gpt4 giày nike

我有一个测试项目,我的简单测试用例扩展了AndroidTestCaseloại:

public class MyTest extends AndroidTestCase{
private Context mContext;

public MyTest(){
siêu();
}

@Ghi đè
public void setUp() throws Exception {
super.setUp();
mContext = getContext();

//Start my service
mContext.startService(new Intent(mContext, MyService.class));
}

@Ghi đè
protected void runTest() {
...
}
...
}

hiện hữusetUp()我上面的测试用例的回调,我开始 MyService .

MyService 也在我的测试项目的 AndroidManifest.xml 中声明:

 <>
android:name="com.my.app.services.MyService"/>

我的服务.java :

public class MyService extends Service {
@Ghi đè
public void onCreate() {
super.onCreate();
Log.d("MyService", "onCreate()");
}

@Ghi đè
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
Log.d("MyService", "onStartCommand");
}
...
}

但是在我运行我的测试用例之后,我从日志中注意到两者都不是 onCreate() 也不 onStartCommand() MyService 的回调已被调用。<强>为什么?是否有任何我错过的适用于 Android 测试框架中服务使用的特殊规则?

câu trả lời hay nhất

AndroidTestCase 返回的上下文可能是模拟上下文——它可能没有 startService 的实现。你读过http://developer.android.com/tools/testing/service_testing.html吗? ?

关于android - 在我的测试用例中启动服务时未调用 onStartCommand(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18717480/

25 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress