English 中文(简体)
RerameingMocktext 需要提供获取资源
原标题:RenamingMockContext needs to provide getResources

我试图建立一个重新命名的模拟信息 来控制我的申请

它的外观如下:

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 looks 如下:

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

当我进行测试时, 它会扔出一个不支持的操作例外。 与下面的堆叠跟踪 。

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)

显然,城市航空机的配置需要一些资源。我需要一些帮助来实施RenaminingMocktext中的资源配置。

问题回答

MockContext 执行为包括获取资源方法在内的大多数方法推出一个不支持的操作例外。 我不喜欢这个解决方案,所以希望别人能提供更好的解决方案。 但我通过在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;
            }
        };
    }
}

您的里程可能各有不同;有些被推翻的方法可能只适用于我的具体应用。





相关问题
Selenium not working with Firefox 3.x on linux

I am using selenium-server , selenium rc for UI testing in my application . My dev box is Windows with FireFox 3.5 and every thing is running fine and cool. But when i try to run selenium tests on my ...

Best browser for testing under Safari Mobile on Linux?

I have an iPhone web app I m producing on a Linux machine. What s the best browser I can use to most closely mimic the feature-limited version of Safari present on the iPhone? (It s a "slimmed down" ...

Code Coverage Tools & Visual Studio 2008 Pro

Just wondering what people are using for code coverage tools when using MS Visual Studio 2008 Pro. We are using the built-in MS test project and unit testing tool (the one that come pre-installed ...

Is there any error checking web app cralwers out there?

Wondering if there was some sort of crawler we could use to test and re-test everything when changes are made to the web app so we know some new change didn t error out any existing pages. Or maybe a ...

热门标签