Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running simple JUnit tests on Android Studio (IntelliJ) when using a Gradle-based configuration

I am using Android Studio/IntelliJ to build on an existing Android project and would like to add some simple JUnit unit tests. What is the right folder to add such tests on?

The android Gradle plug-in defines a directory structure with src/main/java for the main source code and src/instrumentTest/java for Android tests.

Trying to add my JUnit tests in instrumentTest didn't work for me. I can run it as an Android test (that's what that directory seems for) but that's not what I'm looking for - I just want to run a simple JUnit test. I tried creating a JUnit run configuration for this Class but that also didn't work - I'm supposing because I'm using a directory that is flagged as Android Test instead of Source.

If I create a new source folder and marki it as such in Project Structure, this will get wiped next time IntelliJ refreshes the project configuration from the gradle build files.

What is the more appropriate way of configuring JUnit tests in an gradle-based android project on IntelliJ? Which directory structure to use for this?

like image 266
Julian Cerruti Avatar asked Jul 24 '13 21:07

Julian Cerruti


People also ask

How do I run a JUnit test in IntelliJ Gradle?

In your Gradle project, in the editor, create or select a test to run. From the context menu, select Run <test name>. icon in the left gutter. If you selected the Choose per test option, IntelliJ IDEA displays both Gradle and JUnit test runners for each test in the editor.

How does Gradle run JUnit tests?

By default, Gradle will run all tests that it detects, which it does by inspecting the compiled test classes. This detection uses different criteria depending on the test framework used. For JUnit, Gradle scans for both JUnit 3 and 4 test classes.

How do I run a single JUnit test from command line Gradle?

In versions of Gradle prior to 5, the test. single system property can be used to specify a single test. You can do gradle -Dtest. single=ClassUnderTestTest test if you want to test single class or use regexp like gradle -Dtest.


1 Answers

Normally, you can't. Welcome to the world of Android, where all tests must run on a device(except Robolectric).

The main reason is that you don't actually have the framework's sources - even if you convince the IDE to run the test locally, you will immediately get a "Stub! Not implemented" exception. "Why?" you might wonder? Because the android.jar that the SDK gives you is actually all stubbed out - all the classes and methods are there but they all just throw an exception. It's there to provide an API but not there to give you any actual implementation.

There's a wonderful project called Robolectric which implements a lot of the framework just so you can run meaningful tests. Coupled with a good mock framework (e.g., Mockito), it makes your job manageable.

Gradle plugin: https://github.com/robolectric/robolectric-gradle-plugin

like image 179
Delyan Avatar answered Sep 25 '22 08:09

Delyan