-
Notifications
You must be signed in to change notification settings - Fork 56
Tests in Android Studio IntellJ
First step: start a test (I'm expecting you will see an error)
- Edit your run configuration
- Working directory:
$MODULE_DIR$
- Working directory:
- Edit your run configuration and add
Run External Tool
- Before launch -> Add -> Run External Tool -> Add
- Program:
../gradlew
- Parameters:
testDebugClasses
- Working directory:
path-to-test-module
- Program:
- Before launch -> Add -> Run External Tool -> Add
But you will need different setups for unit and component tests. I prefer the more generic version with the cost, that I try to tecomple all test modules. Read further for that ...
Would be great to start tests with just a right click? Then setup a default configuration for all tests. Current the only drawback is, that it try at each test start to recompile component & unit tests.
- Select default Unit Test run configuration
- Working directory:
$MODULE_DIR$
- add
Run External Tool
- Program:
./gradlew
- Parameters:
:UnitTestsRobolectric:testDebugClasses :ComponentTestsRobolectric:testDebugClasses
- Working directory:
path-to-whole-project-root
- Program:
- Working directory:
You must use the custom test runner from espresso. Here I prefer to set up default run configuration.
Open run configuration -> Defaults -> Android Test
- Module: select main module
- Specific instrumentation runner: com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner
Finish.
Unit and component test will need different classpath with an entry to compiled test classes.
- Start a test and you will get error like:
/Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home/bin/java ...
Class not found: "com.example.activities.MainActivityTest"
Process finished with exit code 1
- From the first line copy the -classpath "..." part to an editor
- Append missing class and resource locations at end of classpath
- absolute-path-to-test-module/build/resources/testDebug
- absolute-path-to-test-module/build/test-classes/debug
- Edit your run configuration
- VM options: Paste here your modified classpath
- Working directory:
$MODULE_DIR$ - Before launch -> Add -> Run External Tool -> Add
- Program: ../gradlew
- Parameters: processTestDebugResources testDebugClasses
- Working directory (stable version): path-to-test-module
- Working directory (tricky version):
$ModuleFileDir$ (= module of current focused file in editor tab)
Finish, now your test should run. This must be repeated when classpath changes occur (e.g add new dependency).
since long time not more seen
!!! JUnit version 3.8 or later expected: java.lang.RuntimeException: Stub!
```
When it occur, make the junit4 classpath entry as the first entry in that classpath.