Roboelectric problems with newer versions of gradle and android sdk

Previously I was only able to use roboelectric with gradle just fine. I keep getting the error Error:(6, 17) error: package org.junit does not exist

. I'm not really sure and have a little bit of this.

Below is my build.gradle project:

// 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 build.gradle app:

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 main 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 read below about this and nothing worked here:

https://www.bignerdranch.com/blog/all-in-together-android-studio-gradle-and-robolectric/ - Android studio plugin that will be used will crash on newer versions of Android studio.

https://www.bignerdranch.com/blog/triumph-android-studio-1-2-sneaks-in-full-testing-support/ - it just 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 anyone point me in the right direction? Why can't he detect org.junit

out build.gradle

?

+3


source to share


2 answers


You need to install the build variant test artifact to Unit Tests

.

enter image description here



I no longer need the "main" project, so I deleted it. My build.gradle file for my application 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'
    }
}

      

+1


source


I ran into a problem last day too. I got a solution.

The error is because you created the java folder for your test class as a java folder that looks like blue in android studio. You actually need it in green

For testing, you just need to create a folder called java, android studio will do the rest.

to remove java folder dependency you can remove



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

      

code from build.gradle

then everything will work fine

0


source







All Articles