访问android测试项目中的资源

访问android测试项目中的资源,android,eclipse,unit-testing,Android,Eclipse,Unit Testing,我已经设置了一个运行junit测试的android测试项目。它使用两个eclipse项目“Application”和“ApplicationTest”,其中我的测试在“ApplicationTest”项目中。在我的一个测试中,我需要访问一个文件,如果我将该文件放在SD卡上,并将一个文件对象指向该文件,则效果良好。但是,我希望将该文件作为资源访问,但这似乎不起作用。这就是我所做的: 将文件保存在ApplicationTest/res/raw/myfile.xml 尝试使用以下方法获取它:Inpu

我已经设置了一个运行junit测试的android测试项目。它使用两个eclipse项目“Application”和“ApplicationTest”,其中我的测试在“ApplicationTest”项目中。在我的一个测试中,我需要访问一个文件,如果我将该文件放在SD卡上,并将一个文件对象指向该文件,则效果良好。但是,我希望将该文件作为资源访问,但这似乎不起作用。这就是我所做的:

  • 将文件保存在
    ApplicationTest/res/raw/myfile.xml
  • 尝试使用以下方法获取它:
    InputStream is=getContext().getResources().openRawResource(R.raw.myfile)
但这给了我一个例外:

android.content.res.Resources$NotFoundException: File Hello World, HelloAndroidActivity! from drawable resource ID #0x7f040000 at android.content.res.Resources.openRawResource(Resources.java:823) at android.content.res.Resources.openRawResource(Resources.java:799) at com.quizzer.test.QuestionHandlerTests.testImportQuestions(Tests.java:182) at java.lang.reflect.Method.invokeNative(Native Method) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154) at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448) Caused by: java.io.FileNotFoundException: Hello World, HelloAndroidActivity! at android.content.res.AssetManager.openNonAssetNative(Native Method) at android.content.res.AssetManager.openNonAsset(AssetManager.java:406) at android.content.res.Resources.openRawResource(Resources.java:820) ... 14 more 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.Invokenactive(本机方法) 位于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.OpenNoAssetNative(本机方法) 位于android.content.res.AssetManager.openNonAsset(AssetManager.java:406) 在android.content.res.Resources.openRawResource(Resources.java:820) ... 14多 我的测试类扩展了AndroidTestCase,因此这就是上下文的来源

更新:


因此,问题似乎是在编译期间使用了测试项目中的资源,但在运行时使用了主项目中的资源。我还不确定如何解决这个问题。因此,目前只有当我在测试项目和主项目中使用相同的原始资源时,它才会起作用,这当然是非常愚蠢的。

我建议扩展
,而不是
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()

我导出测试用例如下:

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

并且文件test_image.bmp保存在assets目录中,如果您打算将该资源用于一些与测试相关的工作,而不是ui资源的一部分,那么这是合理的。该技术在这里的另一个上下文中使用:

由于
Android Gradle插件
版本1.1,您不必使用
工具来加载文件资源


我写了如何使用POJO案例来实现它。

使用构建工具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")
        }
    }
}

不清楚你想在这里做什么。或者可能只是我。你能详细说明你在这里想做什么吗。设置很清楚,只需给出一个通过结果和失败结果的示例。@Siddharth我不确定我应该详细说明什么,我的意思是,如果找到资源,我的测试将通过(因为如果我直接从SD卡访问文件,它可以正常工作)。我想我可能错过了一些关于如何在android中使用资源文件,或者如何从单元测试类访问资源文件的内容。那么,如果android测试项目是一个子项目,它会有帮助吗?还是一个独立的项目?谢谢,这很有效。虽然我在创建数据库时遇到了问题,但在这里找到了解决方法:非常感谢您的建议!另外,InstrumentationTestCase似乎也能工作。
getTargetContext()
成功了!Thk感谢您指出这一点。如果您不是在测试
活动,而是需要访问
工具
,我建议扩展
工具测试用例
,而不是
活动测试用例
@plesatejvlk我可以在正常的Junit测试中使用它吗?或者我应该转移到工具测试来使用它吗?我收到一个错误“android.test.InstrumentationTestCase not mocked”,我为InstrumentationTestCase类调用了PowerMockito.mockstatic。我必须将
getContext()
更改为
getTargetContext()
我仍然收到一个错误:android.content.res.Resources$NotFoundException:String resource ID\0x7f06000e