gradle compileJava error: package org.junit does not exist

耗尽温柔 提交于 2019-12-04 22:26:26

So apparently I needed to add a compile dependency and then also declare repositories. My new build.gradle that successfully runs the test:

apply plugin: 'java'

repositories { jcenter() }
dependencies {
    testCompile 'junit:junit:4.12'
    compile 'junit:junit:4.12'
}

sourceSets {
    main {
        java {
            srcDir 'src'
        }
    }
}

I have same issue in my latest android version and I have resolved it using below code. I hope you get help.

dependencies {
    testImplementation 'junit:junit:4.12'
    implementation 'junit:junit:4.12'
}

Try adding

 repositories {
    maven { url 'http://repo1.maven.org/maven2' }

directly under your buildscript{ in build.gradle

I know this is old, but I've run into this recently. You should be able to change the srcdirs for testing with gradle and also have unit tests work. If you want a src/* structure then just put all your tests in test/*.

The issue you're likely experiencing is that if you include your tests in your main/java code folder it will try to compile them during that phase. Move them outside of the src folder and update the srcdir structure accordingly and it should work as expected.

apply plugin: 'java'

repositories { jcenter() }
dependencies {
    testCompile 'junit:junit:4.12'
}

sourceSets {
    main {
        java {
            srcDir 'src'
        }
    }
    test {
        java {
            srcDir 'test'
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!