I have setup an android test project that runs junit tests. It's using two eclipse projects "Application" and "ApplicationTest" where my tests are in the "ApplicationTest" project. In one of my tests I need to access a file, this works fine if I put the file on the sdcard and point a File object to it. However I would like to access the file as a resource, but that does not seem to work. This is what I did:
我已经设置了一个运行junit测试的android测试项目。它正在使用两个eclipse项目“Application”和“ApplicationTest”,我的测试在“ApplicationTest”项目中。在我的一个测试中,我需要访问一个文件,如果我将文件放在SD卡上并将一个File对象指向它,这可以正常工作。但是我想将该文件作为资源访问,但这似乎不起作用。这就是我做的:
- Saved the file in
ApplicationTest/res/raw/myfile.xml
- 将文件保存在ApplicationTest / res / raw / myfile.xml中
- Trying to get it using:
InputStream is = getContext().getResources().openRawResource(R.raw.myfile);
- 试图使用它:InputStream is = getContext()。getResources()。openRawResource(R.raw.myfile);
But that gives me this exception:
但这给了我这个例外:
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
My test class extends AndroidTestCase so that's where the context comes from.
我的测试类扩展了AndroidTestCase,这就是上下文的来源。
Update:
更新:
So the problem seem to be that during compilation the resources in the test project are used, but at runtime the resources in the main project are used. I am yet unsure how to fix that. So currently it only works if I put the same raw resource both in the test project and the main project which of course is quite stupid.
所以问题似乎是在编译期间使用了测试项目中的资源,但是在运行时使用了主项目中的资源。我还不确定如何解决这个问题。因此,目前只有在测试项目和主项目中都使用相同的原始资源才有效,这当然是非常愚蠢的。
4 个解决方案
#1
23
I would suggest extending ActivityTestCase
instead of AndroidTestCase
. You can than access test project resources via
我建议扩展ActivityTestCase而不是AndroidTestCase。您可以通过访问测试项目资源
getInstrumentation().getContext().getResources().openRawResource(R.raw.your_res)
.
getInstrumentation()。的getContext()。getResources()。openRawResource(R.raw.your_res)。
Dummy test case example:
虚拟测试案例:
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);
}
}
And then in test methods use getInstrumentation().getTargetContext()
wherever you used getContext()
in your AndroidTestCase
extension.
然后在测试方法中使用getInstrumentation()。getTargetContext(),只要在AndroidTestCase扩展中使用getContext()。
#2
2
I derived the test case as follows:
我推导出测试用例如下:
class MyTest extends InstrumentationTestCase {
void setUp() {
InputStream is = getInstrumentation().getContext().getAssets()
.open("test_image.bmp");
...
}
}
And the file test_image.bmp is saved in assets directory, which is reasonable if you intend to use the asset for some testing related work - and its not part of ui resources. The technique is used in another context here: https://*.com/a/4570206/1577626
并且文件test_image.bmp保存在assets目录中,如果您打算将该资产用于某些与测试相关的工作 - 这不属于ui资源,这是合理的。该技术在另一个上下文中使用:https://*.com/a/4570206/1577626
#3
#4
0
With build tools 3.0.0 you can use ActivityTestRule
使用构建工具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")
}
}
}
#1
23
I would suggest extending ActivityTestCase
instead of AndroidTestCase
. You can than access test project resources via
我建议扩展ActivityTestCase而不是AndroidTestCase。您可以通过访问测试项目资源
getInstrumentation().getContext().getResources().openRawResource(R.raw.your_res)
.
getInstrumentation()。的getContext()。getResources()。openRawResource(R.raw.your_res)。
Dummy test case example:
虚拟测试案例:
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);
}
}
And then in test methods use getInstrumentation().getTargetContext()
wherever you used getContext()
in your AndroidTestCase
extension.
然后在测试方法中使用getInstrumentation()。getTargetContext(),只要在AndroidTestCase扩展中使用getContext()。
#2
2
I derived the test case as follows:
我推导出测试用例如下:
class MyTest extends InstrumentationTestCase {
void setUp() {
InputStream is = getInstrumentation().getContext().getAssets()
.open("test_image.bmp");
...
}
}
And the file test_image.bmp is saved in assets directory, which is reasonable if you intend to use the asset for some testing related work - and its not part of ui resources. The technique is used in another context here: https://*.com/a/4570206/1577626
并且文件test_image.bmp保存在assets目录中,如果您打算将该资产用于某些与测试相关的工作 - 这不属于ui资源,这是合理的。该技术在另一个上下文中使用:https://*.com/a/4570206/1577626
#3
0
Since Android Gradle Plugin
version 1.1 you haven't to use Instrumentation
to load file resource.
从Android Gradle Plugin 1.1版开始,您不必使用Instrumentation来加载文件资源。
I wrote here how to do it with POJO unit test case.
我在这里写了如何使用POJO单元测试用例。
#4
0
With build tools 3.0.0 you can use ActivityTestRule
使用构建工具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")
}
}
}