3

我正在尝试构建一个 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。使用以下堆栈跟踪。

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)

显然,城市飞艇的配置需要一些资源。我可以在如何在 RenamingMockContext 中实现 getResources 方面使用一些帮助。

4

1 回答 1

2

对于大多数方法,包括 getResources 方法,MockContext 实现都会引发 UnsupportedOperationException。我不喜欢这个解决方案,所以希望其他人提供更好的解决方案。但是我能够通过在 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;
            }
        };
    }
}

你的旅费可能会改变; 一些被覆盖的方法可能只适用于我的特定应用程序。

于 2012-08-13T04:07:45.737 回答