-
Notifications
You must be signed in to change notification settings - Fork 56
Tests in Android Studio IntellJ
This template project works on command-line and with Android Studio without any extra magic configuration.
Would be great to start tests with just a right click? Then setup a default UnitTest configuration. Current the only drawback is that it try to compile all component & unit tests.
- Select default Unit Test run configuration
- Working directory:
$MODULE_DIR$
- At
before launch: Make
, addRun External Tool
- Program:
./gradlew
- Parameters:
testDebugClasses
- Working directory:
$ProjectFileDir$
- Program:
- remove the already existing make step (not more necessary)
- Working directory:
`Class not found: "com.example.activities.DatabaseActivityTest"`` When this message occur start your test again, current workaround force this behaviour to get robolectric tests running inside Android Studio.
Sometimes i have the issue
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.
First step: start a test (I'm expecting you will see an error)
When the test class could not be found, try starting the test a second time. This effect comes through a workaround for missing robolectric support in android studio where the iml file is modified by a gradle task.
- 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 to compile all test modules.
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.