gpt4 book ai didi

android - 使用 Robolectric 测试 HostApduService

转载 作者:太空狗 更新时间:2023-10-29 15:00:49 25 4
gpt4 key购买 nike

我想用 Robolectric 测试来测试我的 HostApduService,但我找不到测试我的服务的方法。测试服务的常规方法不适用于 HostApduServices。有什么建议吗?

到目前为止我尝试了什么:

示例正常服务

public class MyNormalService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}

public void doStuff(){
// Logic
}
}

示例 HostApdu 服务

public class MyHostApduService extends HostApduService {

@Override
public byte[] processCommandApdu(final byte[] commandApdu, Bundle extras) {
// Do my stuff
}
}

测试

@Config(emulateSdk = 18)
@RunWith(RobolectricTestRunner.class)
public class MyHostApduServiceTest {

@Test
public void testNormalService(){ // Succeeds
MyNormalService service = new MyNormalService();
assertNotNull(service);
}

@Test
public void testProcessCommandApdu1(){ // Fails
MyHostApduService service = new MyHostApduService();
assertNotNull(service);
}
@Test

public void testProcessCommandApdu2(){ // Fails
MyHostApduService service = Robolectric.buildService(MyHostApduService.class).create().get();
assertNotNull(service);
}
@Test
public void testProcessCommandApdu3(){ // Fails
MyHostApduService service = Robolectric.setupService(MyHostApduService.class);
assertNotNull(service);
}
}

所有 apdu 测试都会导致相同的错误:

java.lang.RuntimeException: Stub!
at android.nfc.cardemulation.HostApduService.__constructor__(HostApduService.java)
at android.nfc.cardemulation.HostApduService.<init>(HostApduService.java:5)
at com.abc.MyHostApduService.<init>(MyHostApduService.java:

testNormalService 成功。

最佳答案

我使用了 android 支持库中的 Espresso 测试框架。以下是我的基本测试提供成功结果的样子:

@RunWith(AndroidJUnit4.class)
@LargeTest
public class HostApduServiceTest {

@Rule
public UiThreadTestRule uiThreadTestRule = new UiThreadTestRule();

@Test
public void testService() throws Throwable {
uiThreadTestRule.runOnUiThread(new Runnable() {
@Override
public void run() {
MyHostApduService service = new MyHostApduService();
Assert.assertNotNull(service);
}
});


}
}

这是相应的 android 依赖项的样子:

testCompile 'junit:junit:4.12'

compile 'com.android.support:support-annotations:23.1.1'

androidTestCompile ('com.android.support.test:runner:0.5'){
exclude module: 'support-annotations'
}
androidTestCompile ('com.android.support.test.espresso:espresso-core:2.2.2'){
exclude module: 'support-annotations'
}

关于android - 使用 Robolectric 测试 HostApduService,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27039197/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com