相关疑难解决方法(0)

在调用getActivity()之前从Setup()中的TestSuite访问应用程序上下文

我有一个Activity从方法中的Application扩展类(应用程序上下文)中提取对象OnCreate().

单元测试此活动时,所需的对象不存在,因为它是从前一个Activity填充并存储在上述应用程序上下文中.

不用说,当我getActivity()ActivityInstrumentationTestCase2扩展测试用例中调用时,我得到一个空指针异常.

如何在活动开始之前填充上下文并使其可用Activity

更新: 经过一些挖掘后我发现:this.getInstrumentation().getTargetContext()然后将其转换为Application扩展类的类型.但我得到一个类强制转换异常,跟踪指向这个:

04-04 21:02:27.036: INFO/TestRunner(431): started: testIt(edu.rockies.rockies.activity.courses.test.TopicTest)
04-04 21:02:27.126: INFO/TestRunner(431): failed: testIt(edu.rockies.rockies.activity.courses.test.TopicTest)
04-04 21:02:27.126: INFO/TestRunner(431): ----- begin exception -----
04-04 21:02:27.136: INFO/TestRunner(431): java.lang.ClassCastException: android.app.ApplicationContext
04-04 21:02:27.136: INFO/TestRunner(431):     at edu.rockies.rockies.activity.courses.test.TopicTest.setUp(TopicTest.java:27)
04-04 21:02:27.136: INFO/TestRunner(431):     at junit.framework.TestCase.runBare(TestCase.java:125)
04-04 21:02:27.136: INFO/TestRunner(431):     at junit.framework.TestResult$1.protect(TestResult.java:106)
04-04 21:02:27.136: INFO/TestRunner(431):     at junit.framework.TestResult.runProtected(TestResult.java:124)
04-04 21:02:27.136: INFO/TestRunner(431):     at junit.framework.TestResult.run(TestResult.java:109)
04-04 21:02:27.136: INFO/TestRunner(431):     at junit.framework.TestCase.run(TestCase.java:118)
04-04 21:02:27.136: INFO/TestRunner(431):     at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
04-04 …
Run Code Online (Sandbox Code Playgroud)

android unit-testing

31
推荐指数
3
解决办法
2万
查看次数

Android:如何在单元测试期间重置/清除应用程序首选项?

我想从一致的测试环境开始,所以我需要重置/清除我的偏好.这是我到目前为止测试的SetUp.它没有报告任何错误,我的测试通过了,但是没有清除偏好.

我正在测试"MainMenu"活动,但我暂时切换到OptionScreen活动(扩展了Android的PreferenceActivity类.)我确实看到测试在运行期间正确打开了OptionScreen.

 public class MyTest extends ActivityInstrumentationTestCase2<MainMenu> {
Run Code Online (Sandbox Code Playgroud)

...

    @Override
    protected void setUp() throws Exception {
    super.setUp();

    Instrumentation instrumentation = getInstrumentation();
    Instrumentation.ActivityMonitor monitor = instrumentation.addMonitor(OptionScreen.class.getName(), null, false);

    StartNewActivity(); // See next paragraph for what this does, probably mostly irrelevant.
    activity = getActivity();
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(activity);
    settings.edit().clear();
    settings.edit().commit(); // I am pretty sure this is not necessary but not harmful either.
Run Code Online (Sandbox Code Playgroud)

StartNewActivity代码:

    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setClassName(instrumentation.getTargetContext(),
            OptionScreen.class.getName());
    instrumentation.startActivitySync(intent);
    Activity currentActivity = getInstrumentation()
            .waitForMonitorWithTimeout(monitor, 5);
    assertTrue(currentActivity != null); …
Run Code Online (Sandbox Code Playgroud)

android unit-testing preferences android-preferences android-activity

21
推荐指数
1
解决办法
2万
查看次数