Android Studio 1.1, easy setup for junit test

I read there are a lot of extensive answers (like this one ), but the Android world is developing so fast that it seems like they are a bit outdated and the official documentation still applies to Eclipse with ADT.

I am running AS 1.1 and I am trying to configure simple junit tests to run on an emulator without Robolectric. If I do not include junit in my build.gradle , it cannot find @After , @Before and @Test , and I get package org.junit does not exist . After adding

 // unit tests androidTestCompile 'junit:junit:4.11' 

the error becomes

 Error:duplicate files during packaging of APK [...]/app/build/outputs/apk/app-debug-test-unaligned.apk Path in archive: LICENSE.txt Origin 1: [...]/.gradle/caches/modules-2/files-2.1/org.hamcrest/hamcrest-core/1.3/42a25dc3219429f0e5d060061f71acb49bf010a0/hamcrest-core-1.3.jar Origin 2: [...]/.gradle/caches/modules-2/files-2.1/junit/junit/4.11/4e031bb61df09069aeb2bffb4019e7a5034a4ee0/junit-4.11.jar You can ignore those files in your build.gradle: android { packagingOptions { exclude 'LICENSE.txt' } } 

Following the console prompt, excluding LICENSE.txt, it works, but it looks like a hack. So I'm wondering, maybe I missed something? Thanks.

+6
source share
2 answers

Testing support for Android Studio modules comes in version 1.1 Beta 4 ( release announcement ) with the Gradle plugin version 1.1.0-rc1 . Additional information in the white paper .

However, at the moment this is an experimental function. For instance. breaks installDebug Gradle.

For using JUnit in instrumental tests, there is a good guide to the Espresso library and another one covering the new AndroidJUnitRunner.

+3
source

If this is useful, I will set up a boiler plate design that allows the use of unit tests and Espresso tests using switching options. You do not need to use third-party plugins with this.

https://github.com/hitherejoe/Android-Boilerplate

+1
source

Source: https://habr.com/ru/post/982255/


All Articles