Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio 1.1, simple junit test setup

I have read around, there are a number of extensive answers (like this one) but the Android world evolves so fast that they seem to be a bit outdated and the official documentation still refers to Eclipse with ADT.

I am running AS 1.1 and I am trying to setup simple junit tests to run on the emulator, without Robolectric. If I don't include junit in my build.gradle, it can't find @After, @Before and @Test and I get package org.junit does not exist. Upon 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 suggestion of excluding LICENSE.txt, it then works but it feels like a hack. So I'm wondering, am I maybe missing something? Thanks.

like image 762
ticofab Avatar asked Feb 10 '15 10:02

ticofab


2 Answers

Android Studio unit testing support comes in 1.1 Beta 4 (release announcement) with Gradle plugin version 1.1.0-rc1. More info in official document.

However it is experimental feature for now. E.g. it breaks installDebug gradle task.

For using JUnit in instrumentation tests there is good guide for Espresso library and another covering new AndroidJUnitRunner.

like image 183
Kirill Boyarshinov Avatar answered Oct 23 '22 21:10

Kirill Boyarshinov


If it's any use I set up a boiler plate project allowing the use of Unit tests and Espresso tests by the use of switching build variants. You won't need the use of any third party plugins with this.

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

like image 1
Joe Birch Avatar answered Oct 23 '22 21:10

Joe Birch