简体   繁体   中英

InstrumentTest Resources with Android + Gradle

I need to test a custom Adapter which uses a TypedArray from which to create the data. I want to write an instrument test for it (ie AndroidTestCase) which of course needs its own test resources.

After setting up the instrumentTest subfolder with the following structure:

/ instrumentTest
  - src
  / res
    - drawable
    - layout
      # layouts for the tests
    - values
      # resource values where the TypedArray exists

Initially Android Studio (IntelliJ IDEA 13) do not recognize the R references (under PACKAGE_NAME.test.R , but after I run assembleTest they get recognized all right. When I do a connectedInstrumentTest the tests fail because of this error:

android.content.res.Resources$NotFoundException: Array resource ID #0x7f040000
    at android.content.res.Resources.obtainTypedArray(Resources.java:509)

The resource it is failing at is under PACKAGE_NAME.test.R .

Help?

The problem is in AndroidTestCase . The context it provides appears not to be actually usable. Changing to InstrumentationTestCase and getting the resources from

getInstrumentation().getContext().getResources()

Seems to work just fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM