//Service class
MyService extends Service {
private SoundPool mSoundPool;
private int mSoundID;
@Override
public void onCreate(){
mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
mSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
@Override
public void onLoadComplete(final SoundPool soundPool, final int sampleId,
final int status) {
mIsSoundLoaded = true;
}
});
//Here I am initializing soundpool and getting resource not found exception
// It should be context of CustomRobolectricTestRunner
mSoundID = mSoundPool.load(getApplicationContext(),R.raw.btn_click, 1);
}
}
//服务测试类
@RunWith(CustomRobolectricTestRunner.class)
//@RunWith(RobolectricTestRunner.class)
public class MyServiceTest
{
@Test
public void testOnCreate()
{
// String hello = this.activity.getString(R.string.app_name);
// assertThat(hello, equalTo("Plus Box"));
MyService service = new MyService();
service.onCreate(); // Getting exception Resource not found.
//To cross check, I have accessed resource in the following way
//and it working fine. I am able to access resource here.
/* int resourceId = Robolectric
.getShadowApplication()
.getResources()
.getIdentifier("btn_click", "raw",
Robolectric.getShadowApplication().getPackageName());
if (resourceId != 0)
{
// Raw folder contains resource.
System.out.println("The value of Raw ID " + resourceId);
assertTrue(true);
}
else
{
// Raw folder doesn't contain resource.
System.out.println("The value of Raw ID " + resourceId);
assertTrue(false);
}
*/
}
}
我甚至尝试创建自己的自定义测试运行器,因为我的应用程序也有应用程序类(MyApplication)。但是看起来,它总是使用MyApplication上下文,而不是TestApplication。我点击了这个https://groups.google.com/forum/#!topic/robolectric/K2q8xAFfQOA链接。
public class TestApplication extends MyApplication {
@Override
public void onCreate() {
createSession(UserGroup.Unknown, 0, SessionState.Active, "", 0, "");
}
}
public class CustomRobolectricTestRunner extends RobolectricTestRunner
implements TestLifecycle<Application> {
public CustomRobolectricTestRunner(final Class<?> testClass)
throws InitializationError {
super(testClass);
}
@Override
public void afterTest(Method arg0) {
// TODO Auto-generated method stub
}
@Override
public void beforeTest(Method arg0) {
// TODO Auto-generated method stub
}
@Override
public Application createApplication(Method arg0, AndroidManifest manifest) {
TestApplication testApplication = new TestApplication();
ShadowContextWrapper shadowApp = Robolectric.shadowOf( testApplication );
shadowApp.setPackageName("com.example.robolectric");
testApplication.onCreate();
return testApplication;
}
@Override
public void prepareTest(Object arg0) {
// TODO Auto-generated method stub
}
}
任何帮助都会得到高度重视。向您致敬,Yuvi
发布于 2014-04-05 19:04:06
Robolectrial2.3引入了一个类似于ActivityController的ServiceController,来帮助驱动服务的生命周期。
ServiceController<MyService> controller = Robolectric.buildService(MyService.class);
MyService service = controller.attach().create().get()
或者简单地说:
MyService myService = Robolectric.setupService(MyService.class);
此外,如果您希望Robolectric使用您的测试应用程序类,那么如果您的应用程序类被命名为'MyApplication',则将您的测试应用程序类命名为'TestMyApplication‘,并将其放置在您的测试目录下的同一个包中。通过在类名前面加上"Test“,它告诉Robolectric使用TestMyApplication进行测试。(参考:http://robolectric.blogspot.ca/2013/04/the-test-lifecycle-in-20.html)
https://stackoverflow.com/questions/22855287
复制相似问题