gradle compileJava error: package org.junit does not exist

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'
}