Java – resource folder in Android studio unit test

I have a gradle project with the following structure:

project/
    src/
        androidTest/
            java/
        main/
            java/
            res/
            AndroidManifest.xml
    build.gradle

Now I want to add a unit test that uses resources ("raw" or "asset")

I put resources into project / Android test / assets / test_ File and use getcontext() getresources() getAssets(). open(“test_file”); (in androidtestcase)

However, this gives me a FileNotFoundException How to solve this problem?

Solution

It looks like you're trying to create a test unit test because you want to create it in the Android test folder

You can use one of these two lines to get the context in the test:

> Context ctx = InstrumentationRegistry. getTargetContext(); This will give you the context of your application You can use it to get assets such as Src / main / assets. > Context ctx = InstrumentationRegistry. getContext(); This will give you the context to test the application You can use it to get the assets in Src / Android test / assets

If you want to learn more about the assets in unit testing, you can read this post At @ L_ 404_ 1 @ you have an example

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>