[英]Unit Test Service using Junit 4
我正在尝试使用JUNIT4编写Android服务的单元测试。 按照我的服务代码:
class myService extends Service {
// Binder given to clients
private IBinder mBinder = null;
@Override
public void onCreate() {
super.onCreate();
mBinder = new LocalBinder();
}
public int multiply(int x, int y){
return (x*y);
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
public class LocalBinder extends Binder {
public myService getService() {
// Return this instance of LocalService so clients can call public methods.
return myService.this;
}
}
}
和我的单元测试代码:
public class testServiceUlt {
@Rule
public final ServiceTestRule mServiceRule = new ServiceTestRule();
@Test
public void testWithBoundService() throws TimeoutException, InterruptedException {
// Create the service Intent.
Intent serviceIntent =
new Intent(InstrumentationRegistry.getTargetContext(), myService.class);
// Bind the service and grab a reference to the binder.
IBinder binder = mServiceRule.bindService(serviceIntent);
// Get the reference to the service, or you can call public methods on the binder directly.
myService service = ((myService.LocalBinder) binder).getService();
int val = service.multiply(80, 0);
assertEquals("should be zero", 0, val);
}
}
问题是活页夹为Null,并且出现以下错误:java.lang.NullPointerException:尝试调用虚拟方法'com.example.xxx.xxx.xxx com.example.xxx.xxx.xxx $ LocalBinder.getService( )'在空对象引用上
您能告诉我什么问题吗? PS-我知道如何将Junit3与ServiceTestCase一起使用,但是我想与Junit4一起使用
谢谢Zachi
我设法解决了这个问题。 我的AndroidManifest.xml文件错误地不包含服务。
就我而言,问题是我在androidTest Manifest文件中声明了Service,由于任何原因它均无法正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.