简体   繁体   中英

Android Studio gradle experimental test (JUNit)

I'm trying to test my class with InstrumentationTestCase. I've been checking the examples but I guess I'm missing something because "Test Artifact" dropdown is disabled and cannot be changed to JUNit.

I declared the libraries in gradle

// Test Compile
testCompile 'junit:junit:4.12'
testCompile 'org.robolectric:robolectric:2.4'
testCompile 'org.mockito:mockito-core:1.9.5'
testCompile 'org.reflections:reflections:0.9.9'

declared app id,

defaultConfig {
    testApplicationId "myproject.android.test"
  }

and getting the latest gradle version

dependencies {
        classpath 'com.android.tools.build:gradle:+'
    }

also created a build configuration for my test package. But when I run the test it gives me

Exception in thread "main" java.lang.NoClassDefFoundError: junit/textui/ResultPrinter

probably because Test Artifact is "Android Instrumentation Tests" and the build configuration is JUNit configuration. Test Artifact dropdown is disabled so I cannot change it to JUNit. Is there something that I'm missing?

Edit: my package tree is like this,

myproject
 - app
   - main
    - java
      -myproject
       -android
        -mypackage
         -myclass.java
   -test
    -java
     -myproject
      -android
       -mypackage
        -test
         -myclasstest.java

and in Android Studio preferences, under Gradle->Experimental, "Enable Unit Testing support" is checked.

You need to do a few steps to activate JVM unit tests in Android Studio. Follow this steps: https://sites.google.com/a/android.com/tools/tech-docs/unit-testing-support

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