Skip to content

Tests in Android Studio IntellJ

Nico Küchler edited this page Aug 30, 2015 · 22 revisions

OBSOLETE

This template project works on command-line and with Android Studio without any extra magic configuration.

Setup Unit & Component tests (Robolectric)

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, add Run External Tool
      • Program: ./gradlew
      • Parameters: testDebugClasses
      • Working directory: $ProjectFileDir$
    • remove the already existing make step (not more necessary)

`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 $ProjectFileDir$ is not set. I must close and reopen the Project, then all is fine. You can avoid it if you write hard path for it.

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) Robolectric: How to do it for each single test

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.

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 to compile all test modules.

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