RenamingMockContext需要提供getResources

时间:2012-05-25 17:37:51

标签: android testing resources android-context

我正在尝试构建一个RenamingMockContext来控制MyAppliction。

看起来如下:

public class RenamingMockContext extends RenamingDelegatingContext {
    private static final String PREFIX = "test.";

    public RenamingMockContext(Context context) {
        super(new DelegatedMockContext(context), PREFIX);
    }

    private static class DelegatedMockContext extends MockContext {
        private Context mDelegatedContext;

        public DelegatedMockContext(Context context) {
            mDelegatedContext = context;
        }

        @Override
        public String getPackageName() {
            return mDelegatedContext.getPackageName();
        }
    }
}

MyApplication如下所示:

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        AirshipConfigOptions options = AirshipConfigOptions.loadDefaultOptions(this);
        UAirship.takeOff(this, options);
        PushManager.enablePush();
        PushManager.shared().setIntentReceiver(UrbanAirshipReceiver.class);
    }
}

当我运行测试时,它会抛出UnsupportedOperationException。使用以下stacktrace。

java.lang.UnsupportedOperationException
at com.carplink.aCarpLink.test.RenamingMockContext$DelegatedMockContext.getResources(RenamingMockContext.java:29)
at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
at com.urbanairship.Options.loadFromProperties(Unknown Source)
at com.urbanairship.Options.loadFromProperties(Unknown Source)
at com.urbanairship.AirshipConfigOptions.loadDefaultOptions(Unknown Source)
at com.carplink.aCarpLink.CarpLinkApplication.onCreate(CarpLinkApplication.java:15)
at android.test.ApplicationTestCase.createApplication(ApplicationTestCase.java:122)
at com.carplink.aCarpLink.test.CarpLinkApplicationTest.setUp(CarpLinkApplicationTest.java:24)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:520)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1606)

显然,Urban Airship的配置需要一些资源。我可以使用一些帮助来解决如何在RenamingMockContext中实现getResources。

1 个答案:

答案 0 :(得分:2)

MockContext实现为大多数方法抛出UnsupportedOperationException,包括getResources方法。我不喜欢这个解决方案,所以希望别人能给出更好的解决方案。但我能够通过在MockContext和MockResources中实现某些方法来克服异常:

private class MyMockContext extends MockContext {
    @Override
    public Resources getResources() {
        return new MockResources() {
            @Override
            public DisplayMetrics getDisplayMetrics() {
                return new DisplayMetrics();
            }
            @Override
            public Configuration getConfiguration() {
                return new Configuration();
            }
            @Override
            public void getValue (int id, TypedValue outValue, boolean resolveRefs) {
                return;
            }
            @Override
            public boolean getBoolean(int id) {
                return true;
            }
            @Override
            public int getDimensionPixelSize(int id) {
                return 0;
            }
        };
    }
}

您的里程可能会有所不同;一些重写的​​方法可能仅适用于我的特定应用程序。