Gradle cannot find JUnit TestCase

apply plugin: ‘java’
apply plugin: ‘eclipse’
apply plugin: ‘groovy’
apply plugin: ‘application’
apply plugin: ‘war’

sourceCompatibility = 1.7
targetCompatibility = 1.7

version = ‘1.0’

mainClassName=‘res-web’

dependencies {

compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
compile group:'org.codehaus.groovy',name:'groovy-all', version:'2.4.5'
compile group:'org.springframework',name:'spring-messaging',version:'4.2.1.RELEASE'
compile group:'org.springframework',name:'spring-beans',version:'4.2.1.RELEASE'
compile group:'org.springframework',name:'spring-context',version:'4.2.1.RELEASE'
compile group:'org.springframework.integration',name:'spring-integration-core',version:'4.2.1.RELEASE'
compile group:'org.springframework.integration',name:'spring-integration-jms',version:'4.2.1.RELEASE'
compile group:'org.springframework',name:'spring-jms',version:'4.2.1.RELEASE'
compile group:'ca.uhn.hapi',name:'hapi-base',version:''
compile group:'ca.uhn.hapi',name:'hapi-structures-v25',version:'2.2'
compile group:'org.hibernate',name:'hibernate-core',version:'4.3.11.Final'
compile group:'org.hibernate',name:'hibernate-validator',version:'5.2.2.Final'
compile group:'javax.validation',name:'validation-api',version:'1.1.0.Final'
compile group:'org.springframework',name:'spring-orm',version:'4.2.1.RELEASE'
compile group:'c3p0',name:'c3p0',version:'0.9.1.2'
compile group:'org.springframework.integration',name:'spring-integration-http',version:'4.2.1.RELEASE'
compile group:'org.apache.activemq',name:'activemq-broker',version:'5.11.1'
compile group:'org.apache.activemq',name:'activemq-kahadb-store',version:'5.11.1'
compile group:'javax.jms',name:'javax.jms-api',version:'2.0.1'
testRuntime group: 'junit', name: 'junit', version: '4.11'
testCompile group:'org.assertj',name:'assertj-core',version:'2.2.0'
testRuntime group:'org.jboss.logging',name:'jboss-logging',version:'3.3.0.Final'
testRuntime group:'javax.el',name:'javax.el-api',version:'2.2.4'
testRuntime group:'org.glassfish.web',name:'javax.el',version:'2.2.4'

}
repositories{
mavenCentral()
flatDir {
dirs “…/Libraries/clib”
}
}

uploadArchives {
repositories {
flatDir {
dirs ‘repos’
}
}
}

C:\Users\bdav\workspace\ecb2bva-4>gradle build
:compileJava
:compileGroovy
startup failed:
General error during conversion: java.lang.NoClassDefFoundError: junit/framework
/TestCase

java.lang.RuntimeException: java.lang.NoClassDefFoundError: junit/framework/Test
Case
at org.codehaus.groovy.control.CompilationUnit.convertUncaughtExceptionT
oCompilationError(CompilationUnit.java:1091)
at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(
CompilationUnit.java:1069)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(Compilat
ionUnit.java:591)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(Co
mpilationUnit.java:569)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.j
ava:546)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.j
ava:525)
at org.gradle.api.internal.tasks.compile.ApiGroovyCompiler.execute(ApiGr
oovyCompiler.java:164)
at org.gradle.api.internal.tasks.compile.ApiGroovyCompiler.execute(ApiGr
oovyCompiler.java:52)
at org.gradle.api.internal.tasks.compile.daemon.CompilerDaemonServer.exe
cute(CompilerDaemonServer.java:53)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionD
ispatch.java:35)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionD
ispatch.java:24)
at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(Messa
geHub.java:360)
at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.
onExecute(ExecutorPolicy.java:54)
at org.gradle.internal.concurrent.StoppableExecutorImpl$1.run(StoppableE
xecutorImpl.java:40)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.
java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor
.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoClassDefFoundError: junit/framework/TestCase
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:14
2)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
:compileGroovy FAILED
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)

    at java.lang.ClassLoader.loadClass(ClassLoader.java:412)

FAILURE: Build failed with an exception.
at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:677)

    at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:545)
  • What went wrong:
    at org.codehaus.groovy.control.ClassNodeResolver.tryAsLoaderClassOrScrip
    t(ClassNodeResolver.java:185)
    Execution failed for task ‘:compileGroovy’.
    at org.codehaus.groovy.control.ClassNodeResolver.findClassNode(ClassNode
    Resolver.java:170)

Compilation failed; see the compiler error output for details.
at org.codehaus.groovy.control.ClassNodeResolver.resolveName(ClassNodeRe
solver.java:126)

    at org.codehaus.groovy.control.ResolveVisitor.resolveToOuter(ResolveVisi

tor.java:678)

  • Try:
    at org.codehaus.groovy.control.ResolveVisitor.resolve(ResolveVisitor.jav
    a:310)
    Run with --stacktrace option to get the stack trace. Run with --info or --debug
    option to get more log output.
    at org.codehaus.groovy.control.ResolveVisitor.visitClass(ResolveVisitor.
    java:1220)

      at org.codehaus.groovy.control.ResolveVisitor.startResolving(ResolveVisi
    

tor.java:178)
BUILD FAILED
at org.codehaus.groovy.tools.javac.JavaAwareCompilationUnit$1.call(JavaA
wareCompilationUnit.java:71)

    at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(

CompilationUnit.java:1055)
Total time: 42.823 secs
… 19 more
Caused by: java.lang.ClassNotFoundException: junit.framework.TestCase
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClar.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
… 41 more

1 error

You might try changing testRuntime to testCompile for the junit dependency.

I had it at testCompile but that did not work either, that’s when I changed to testRuntime.

It seems you have your test in your main sourceSet as compileGroovy complains about missing dependencies. So you either move your junit tests from src/main/groovy into src/test/groovy or you move your junit dependency into the compile configuration.

My bad, I merged my code into existing code repo in git. That project had tests in its own test package which I wasn’t aware of. Many many thanks, your advice made me to check the whole project. I’ve identified that issue, modified the sourcesets and the issue is fixed.