Skip to content

Tests in Android Studio IntellJ

Nico Küchler edited this page Jun 4, 2014 · 22 revisions

Setup Unit & Component tests (Robolectric)

How to do it for each single test module

First step: start a test (I'm expecting you will see an error)

Fix the Invalid byte 1 of 1-byte UTF-8 sequence.

  • Edit your run configuration
    • Working directory: $MODULE_DIR$

Force recompile of test classes

  1. 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

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 ...

How to do it once for all

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

Acceptance tests (Espresso)

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.

(OBSOLETE) Setup Unit & Component tests (Robolectric)

Unit and component test will need different classpath with an entry to compiled test classes.

  1. 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
  1. From the first line copy the -classpath "..." part to an editor
  2. 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
  3. 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.
Clone this wiki locally