【问题标题】:Getting resource not found exception using robolectric使用 robolectric 获取资源未找到异常
【发布时间】:2014-05-16 07:46:12
【问题描述】:
//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

    }

}

任何帮助都会受到高度赞赏。 问候,尤维

【问题讨论】:

    标签: android unit-testing junit4 robolectric


    【解决方案1】:

    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”,请将您的测试应用程序类命名为“TestMyApplication”,并将其放在测试目录下的同一包中。通过在类名前加上“Test”,它告诉 Robolectric 使用 TestMyApplication 进行测试。 (参考:http://robolectric.blogspot.ca/2013/04/the-test-lifecycle-in-20.html

    【讨论】:

    • 嗨,你错误地解释了我的问题。我添加了更多代码 sn-p。我不会测试或模拟来自 Android 框架的 SoudPool 相关类。我想做 MyService 类的单元测试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-02
    • 2017-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-15
    • 1970-01-01
    相关资源
    最近更新 更多