使用robolectric获取资源未找到异常

时间:2014-04-04 06:35:54

标签: android unit-testing junit4 robolectric

//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);
        }
    */


    }


}

即使我尝试创建自己的自定义测试运行器,因为我的应用程序也有Application类(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

1 个答案:

答案 0 :(得分:0)

Robolectric 2.3引入了一个类似于ActivityController的ServiceController来帮助推动服务的生命周期。

ServiceController<MyService> controller = Robolectric.buildService(MyService.class);
MyService service = controller.attach().create().get()

或者简单地说:

MyService myService = Robolectric.setupService(MyService.class);

此外,如果您希望Robolectric使用您的测试应用程序类 - 那么如果您的应用程序类被命名为“MyApplication&#39;”,请为您的测试应用程序类命名&#39; TestMyApplication&#39;并将其放在测试目录下的同一个包中。通过预先考虑&#34;测试&#34;对于类名,它告诉Robolectric使用TestMyApplication进行测试。 (参考:http://robolectric.blogspot.ca/2013/04/the-test-lifecycle-in-20.html