KVISH
KVISH

Reputation: 13208

Roboelectric issues with new gradle and android sdk versions

I used to be able to use roboelectric with gradle just fine until recently. I keep getting an error Error:(6, 17) error: package org.junit does not exist. I'm not quite sure and have dug into this quite a bit.

Below is my project build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.2.3'
    }
}

allprojects {
    repositories {
        mavenCentral()
        jcenter()
    }
}

Below is my app build.gradle:

repositories {
    mavenCentral()
    jcenter()
}

apply plugin: 'com.android.application'

android {
    ...

    sourceSets {
        androidTest.setRoot('src/test')
    }
}


dependencies {
    ...

    // Testing
    compile project(':core')
    testCompile 'org.robolectric:robolectric:2.4'
    testCompile 'junit:junit:4.+'
    testCompile 'org.easytesting:fest:1.0.16'
    testCompile 'com.squareup:fest-android:1.0.8'
}

my core project build.gradle:

apply plugin: 'java'

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    testCompile 'org.robolectric:robolectric:2.4'
}

I have read the below regarding this and nothing here has worked for me:

https://www.bignerdranch.com/blog/all-in-together-android-studio-gradle-and-robolectric/ - the android studio plugin that is to be used crashes on newer android studio versions.

https://www.bignerdranch.com/blog/triumph-android-studio-1-2-sneaks-in-full-testing-support/ - this simply doesn't solve the problem. It cannot find org.junit.

https://discuss.gradle.org/t/why-cant-my-simplest-one-line-build-gradle-compile-tests-with-junit-jar/1868

Can anybody point me in the right direction for this? Why is it not able to detect org.junit from the build.gradle?

Upvotes: 3

Views: 933

Answers (2)

Arun Antoney
Arun Antoney

Reputation: 4382

last day i am also facing the problem.Atlast i got the solution.

The error is due to you created the java folder for your test class as java folder which appear blue color in android studio.Actually you need it in green color

For testing you just need to create folder with java name,everything else will do by android studio itself.

for removing java folder dependency you can remove

  sourceSets {
    androidTest.setRoot('src/test')
}

code from the build.gradle

then everything will work fine

Upvotes: 0

KVISH
KVISH

Reputation: 13208

You have to set the build variant test artifact to Unit Tests.

enter image description here

I no longer needed the "core" project, so I deleted it. My build.gradle for my app looks like this:

repositories {
    jcenter()
}

apply plugin: 'com.android.application'

android {
    ...
    sourceSets {
        androidTest.setRoot('src/test')
    }
}

dependencies {
    ...

    // Testing
    testCompile 'junit:junit:4.12'
    testCompile 'org.easytesting:fest:1.0.16'
    testCompile 'com.squareup:fest-android:1.0.8'
    testCompile('org.robolectric:robolectric:3.0-rc2') {
        exclude group: 'commons-logging', module: 'commons-logging'
        exclude group: 'org.apache.httpcomponents', module: 'httpclient'
    }
}

Upvotes: 1

Related Questions