Mark Gilchrist
Mark Gilchrist

Reputation: 2032

Mockito and JUnit not found

I have a very old project that has started out as a an eclipse project and is now gradle/ android studio

Currently I have just one test which is stored in the path

src/test/java/

I have the below gradle file

buildscript {
    ext.kotlin_version = '1.2.41'
    ext.latest_google_version = '27.1.1'
    repositories {
        jcenter()
        maven { url 'https://maven.fabric.io/public' }
        maven { url 'https://dl.bintray.com/jetbrains/anko' }
        mavenCentral()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.2'
        classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'io.fabric.tools:gradle:1.24.1'
    }
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'io.fabric'



kotlin {
    experimental {
        coroutines 'enable'
    }
}

repositories {
    maven { url "https://jitpack.io" }
    maven { url 'https://maven.fabric.io/public' }
    maven { url 'https://dl.bintray.com/jetbrains/anko' }
    mavenCentral()
    google()
    jcenter()
}

dependencies {
    implementation fileTree(dir: 'libs', include: '*.jar')
    implementation('com.crashlytics.sdk.android:crashlytics:2.8.0@aar') {
        transitive = true
    }
    implementation('com.crashlytics.sdk.android:answers:1.4.1@aar') {
        transitive = true
    }
    implementation 'com.android.support:multidex:1.0.3'
    implementation 'com.squareup.okhttp3:okhttp:3.10.0'
    implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
    implementation 'io.reactivex.rxjava2:rxjava:2.1.9'

    implementation 'com.jakewharton.rxbinding:rxbinding:0.4.0'
    implementation 'com.jakewharton.rxbinding:rxbinding-support-v4:0.4.0'
    implementation 'io.reactivex.rxjava2:rxkotlin:2.2.0'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation "org.jetbrains.anko:anko:0.10.3"
    implementation "com.android.support:support-v13:$latest_google_version"
    implementation "com.android.support:appcompat-v7:$latest_google_version"
    implementation "com.android.support:support-v4:$latest_google_version"
    implementation "com.android.support:support-core-utils:$latest_google_version"
    implementation "com.android.support:design:$latest_google_version"
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'

    // UnitTest frameworks
    testImplementation 'junit:junit:4.12'
    testImplementation "org.mockito:mockito-core:2.18.0"
    testImplementation "org.robolectric:robolectric:3.8"

    // androidTest frameworks
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test:rules:1.0.2'

}

android {
    compileSdkVersion 27
    buildToolsVersion '27.0.3'
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    defaultConfig {
        multiDexEnabled true
    }
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/ASL2.0'
        exclude 'META-INF/rxjava.properties'
    }
    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
//            resources.srcDirs = ['src']
//            aidl.srcDirs = ['src']
//            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
//            assets.srcDirs = ['assets']

        }

        // Move the tests to tests/java, tests/res, etc...
        androidTest.setRoot('tests')

        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
}

If I run my unit tests with the gradle file as above is I get errors of

Unresolved reference: junit 
Unresolved reference: junit 
Unresolved reference: mockito   
Unresolved reference: mockito   
Unresolved reference: mockito   
Unresolved reference: mockito   
Unresolved reference: Mock  
Unresolved reference: Before    
Unresolved reference: MockitoAnnotations    
Unresolved reference: Test  
Unresolved reference: verify    
Unresolved reference: verifyNoMoreInteractions  
Unresolved reference: Test  
Unresolved reference: Test

This is effectively everywhere where junit is referenced in the test.

If I change

// UnitTest frameworks
testImplementation 'junit:junit:4.12'
testImplementation "org.mockito:mockito-core:2.18.0"
testImplementation "org.robolectric:robolectric:3.8"

to

// UnitTest frameworks
implementation 'junit:junit:4.12'
implementation "org.mockito:mockito-core:2.18.0"
implementation "org.robolectric:robolectric:3.8"

The tests compile but the test runner will complain that the suite of tests is empty.

What am I doing wrong?

Upvotes: 2

Views: 2230

Answers (2)

David Rawson
David Rawson

Reputation: 21497

This kind of error can be caused by configuring your source sets incorrectly. In your build.gradle you have the following:

sourceSets {
    main {
        manifest.srcFile 'AndroidManifest.xml'
        java.srcDirs = ['src']
    //snip
    }

    // Move the tests to tests/java, tests/res, etc...
    androidTest.setRoot('tests')     
}

In other words, your Android tests (instrumentation tests) are in src/tests/java/com/example. Note you also have the main source set configured as all of src/* so this is also incorrect - it should be just src/main

Make sure you have correctly understood the difference between local unit tests (which normally go in the test directory) and instrumented unit tests (which normally go in the androidTest directory) and you can also try adding the following closure to your build.gradle:

main {
    manifest.srcFile 'AndroidManifest.xml'
    java.srcDirs = ['src/main'] 
}

test {
    java.srcDirs = ['src/test']
}

Then you may need to perform a Gradle sync, if that doesn't work, you can try File -> Invalidate caches/restart.

Good luck!

Upvotes: 2

tynn
tynn

Reputation: 39873

The reason for your errors is the configuration

main {
    java.srcDirs = ['src']
}

Here you define the whole src directory as to contain Java main sources. The default directory for this would be src/main/java.

Since your tests are stored within src/test/java, these are contained in the main sources as well and thus you needed to change the configuration from testImplementation to implementation to compile.

Instead of configuring your source sets manually, you should consider to restructure your project to meet the default setup.

Upvotes: 5

Related Questions