【问题标题】:Accessing resources in an android test project访问android测试项目中的资源
【发布时间】:2012-03-04 05:15:38
【问题描述】:

我已经设置了一个运行 junit 测试的 android 测试项目。它使用两个 Eclipse 项目“Application”和“ApplicationTest”,我的测试在“ApplicationTest”项目中。在我的一个测试中,我需要访问一个文件,如果我将文件放在 sdcard 上并将 File 对象指向它,这可以正常工作。但是我想将该文件作为资源访问,但这似乎不起作用。这就是我所做的:

  • 将文件保存在ApplicationTest/res/raw/myfile.xml
  • 尝试使用:InputStream is = getContext().getResources().openRawResource(R.raw.myfile);

但这给了我这个例外:

android.content.res.Resources$NotFoundException:文件 Hello World,HelloAndroidActivity!来自可绘制资源 ID #0x7f040000 在 android.content.res.Resources.openRawResource(Resources.java:823) 在 android.content.res.Resources.openRawResource(Resources.java:799) 在 com.quizzer.test.QuestionHandlerTests.testImportQuestions(Tests.java:182) 在 java.lang.reflect.Method.invokeNative(Native Method) 在 android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169) 在 android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154) 在 android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529) 在 android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448) 引起:java.io.FileNotFoundException:Hello World,HelloAndroidActivity! 在 android.content.res.AssetManager.openNonAssetNative(本机方法) 在 android.content.res.AssetManager.openNonAsset(AssetManager.java:406) 在 android.content.res.Resources.openRawResource(Resources.java:820) ... 14 更多

我的测试类扩展了 AndroidTestCase,所以这就是上下文的来源。

更新:

所以问题似乎是在编译期间使用了测试项目中的资源,但在运行时使用了主项目中的资源。我还不确定如何解决这个问题。所以目前它只有在我在测试项目和主项目中都放置相同的原始资源时才有效,这当然很愚蠢。

【问题讨论】:

  • 不清楚你想在这里做什么。或者可能只是我。你能详细说明你想在这里做什么。设置很清楚,只需举例说明您对 PASS 结果和 FAIL 结果的期望。
  • @Siddharth 我不确定我应该详细说明什么,我的意思是如果找到资源,我的测试将通过(因为如果我直接从 sdcard 访问文件,它工作正常)。我想我可能错过了一些关于如何在 android 中使用资源文件,或者如何从单元测试类中访问它们的内容。
  • 那么如果android测试项目是子项目会有帮助吗?还是依赖项目?

标签: android eclipse unit-testing


【解决方案1】:

使用构建工具 3.0.0,您可以使用 ActivityTestRule

@RunWith(AndroidJUnit4::class)
@SmallTest
class MainActivityTest {
    @Rule
    @JvmField
    var mainActivityRule = ActivityTestRule(MainActivity::class.java)

    private val baseUrl: String
        get() {
            return mainActivityRule.activity.getString(R.string.base_url)
        }

    @Test
    fun launchingWithMovieIntent() {
            assert.that(baseUrl, equalTo("someValue")
        }
    }
}

【讨论】:

    【解决方案2】:

    Android Gradle Plugin 1.1 版以来,您无需使用Instrumentation 加载文件资源。

    我写了here 如何使用 POJO unit test 案例。

    【讨论】:

      【解决方案3】:

      我导出的测试用例如下:

      class MyTest extends InstrumentationTestCase {
          void setUp() {
              InputStream is = getInstrumentation().getContext().getAssets()
                  .open("test_image.bmp");
              ...
         }
      }
      

      并且文件test_image.bmp 保存在assets 目录中,如果您打算将该asset 用于一些与测试相关的工作,这是合理的——它不是ui 资源的一部分。该技术在此处的另一个上下文中使用:https://stackoverflow.com/a/4570206/1577626

      【讨论】:

      • 我不得不将 getContext() 更改为 getTargetContext()
      • 我仍然收到错误:android.content.res.Resources$NotFoundException: String resource ID #0x7f06000e
      【解决方案4】:

      我建议扩展@987654321@ 而不是AndroidTestCase。您可以通过

      访问测试项目资源

      getInstrumentation().getContext().getResources().openRawResource(R.raw.your_res).

      虚拟测试用例示例:

      public class Test extends ActivityTestCase {
      
         public void testFoo() {  
      
            // .. test project environment
            Context testContext = getInstrumentation().getContext();
            Resources testRes = testContext.getResources();
            InputStream ts = testRes.openRawResource(R.raw.your_res);
      
            assertNotNull(testRes);
         }    
      }
      

      然后在测试方法中使用getInstrumentation().getTargetContext(),无论您在AndroidTestCase 扩展中使用getContext()

      【讨论】:

      • 感谢这工作。我确实在创建数据库时遇到了问题,但在这里找到了解决方案:stackoverflow.com/a/8488722/11722
      • 非常感谢您的建议!附言InstrumentationTestCase 似乎也可以工作。
      • getTargetContext() 成功了!谢谢指出。
      • 如果您不测试Activity,但需要访问Instrumentation,我建议您扩展InstrumentationTestCase 而不是ActivityTestCase
      • @plesatejvlk 我可以在正常的 Junit 测试中使用它,还是我需要转移到 Instrumentation 测试才能使用它?我收到一个错误“android.test.InstrumentationTestCase not mocked”,我为 InstrumentationTestcase 类调用了 PowerMockito.mockstatic。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-05
      • 1970-01-01
      相关资源
      最近更新 更多