[groovy-user] Problem using JUnit 4, NoClassDefFound: org/hamcrest/Description

8 views
Skip to first unread message

Jonathan Bernard

unread,
Feb 17, 2010, 10:17:15 AM2/17/10
to us...@groovy.codehaus.org
Hello,

I am trying to do some unit testing with Groovy 1.7.0 and JUnit 4.7
(from $GROOVY_HOME/lib). When I try to compile my test classes I
receive the following error:

java.lang.NoClassDefFoundError: org/hamcrest/Description
at java.lang.Class.getDeclaredFields0(Native Method)
at java.lang.Class.privateGetDeclaredFields(Class.java:2291)
at java.lang.Class.getDeclaredFields(Class.java:1743)
at org.codehaus.groovy.vmplugin.v5.Java5.configureClassNode(Java5.java:306)
at org.codehaus.groovy.ast.ClassNode.lazyClassInit(ClassNode.java:263)
at org.codehaus.groovy.ast.ClassNode.getDeclaredMethods(ClassNode.java:830)
at org.codehaus.groovy.ast.ClassNode.getMethods(ClassNode.java:845)
at org.codehaus.groovy.ast.ClassNode.hasPossibleStaticMethod(ClassNode.java:1217)
at org.codehaus.groovy.control.StaticImportVisitor.findStaticMethod(StaticImportVisitor.java:368)
at org.codehaus.groovy.control.StaticImportVisitor.findStaticMethodImportFromModule(StaticImportVisitor.java:344)
at org.codehaus.groovy.control.StaticImportVisitor.transformMethodCallExpression(StaticImportVisitor.java:177)
at org.codehaus.groovy.control.StaticImportVisitor.transform(StaticImportVisitor.java:77)
at org.codehaus.groovy.ast.ClassCodeExpressionTransformer.visitExpressionStatement(ClassCodeExpressionTransformer.java:139)
at org.codehaus.groovy.ast.stmt.ExpressionStatement.visit(ExpressionStatement.java:40)
at org.codehaus.groovy.ast.CodeVisitorSupport.visitBlockStatement(CodeVisitorSupport.java:35)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitBlockStatement(ClassCodeVisitorSupport.java:161)
at org.codehaus.groovy.ast.stmt.BlockStatement.visit(BlockStatement.java:51)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClassCodeContainer(ClassCodeVisitorSupport.java:97)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitConstructorOrMethod(ClassCodeVisitorSupport.java:108)
at org.codehaus.groovy.ast.ClassCodeExpressionTransformer.visitConstructorOrMethod(ClassCodeExpressionTransformer.java:50)
at org.codehaus.groovy.control.StaticImportVisitor.visitConstructorOrMethod(StaticImportVisitor.java:56)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitMethod(ClassCodeVisitorSupport.java:119)
at org.codehaus.groovy.ast.ClassNode.visitContents(ClassNode.java:1044)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClass(ClassCodeVisitorSupport.java:50)
at org.codehaus.groovy.control.StaticImportVisitor.visitClass(StaticImportVisitor.java:51)
at org.codehaus.groovy.control.CompilationUnit$7.call(CompilationUnit.java:593)
at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:957)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:517)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:495)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:472)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:456)
at org.codehaus.groovy.tools.FileSystemCompiler.compile(FileSystemCompiler.java:57)
at org.codehaus.groovy.tools.FileSystemCompiler.doCompilation(FileSystemCompiler.java:170)
at org.codehaus.groovy.ant.Groovyc.compile(Groovyc.java:816)
at org.codehaus.groovy.ant.Groovyc.execute(Groovyc.java:544)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)

This is easily reproduced with the following build file and source:

build.xml:
==========

<project name="Error Example" default="compile-test" basedir=".">
<property environment="env"/>

<path id="classpath">
<fileset dir="${env.GROOVY_HOME}/lib">
<include name="**/*.jar"/>
</fileset>
</path>

<taskdef name="groovyc"
classname="org.codehaus.groovy.ant.Groovyc"
classpathref="classpath"/>

<target name="compile-test">
<groovyc
srcdir="${basedir}"
destdir="${basedir}"
classpathref="classpath"/>
</target>
</project>


error/example/ExampleTest.groovy:
=================================

package error.example

import org.junit.Test
import static org.junit.Assert.assertTrue

class ExampleTest {

@Test test1() {
assertTrue true
}
}

It seems that the

import static org.junit.Assert.assertTrue

is the trigger for the problem, but I do not understand what the
source of the problem is. There are no other JUnit jars on the
classpath, and the junit-4.7.jar contains the Hamcrest classes.
/org/hamcrest/Description.class specifically is in the jar. The
compile error persists even when I extract the junit jar and
specifically add the Hamcrest classes to the classpath.

I am not even using the hamcrest functionality. Anybody know what is going on?

java -version:
java version "1.6.0_15"
Java(TM) SE Runtime Environment (build 1.6.0_15-b03)
Java HotSpot(TM) Server VM (build 14.1-b02, mixed mode)

groovy -version:
Groovy Version: 1.7.0 JVM: 1.6.0_15

OS version:
Ubuntu 9.10

Thanks,
Jonathan Bernard

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


Roshan Dawrani

unread,
Feb 17, 2010, 11:41:15 AM2/17/10
to us...@groovy.codehaus.org
Couldn't reproduce the issue locally, unfortunately. Your build.xml and groovy file both worked on a similar environment I have (but on Windows XP)

Can you run the following build.xml and share its output? Let's see if it gives any hint.
------------------------------------------------------------------------------------
<project name="Error Example" default="junit" basedir=".">

   <property environment="env"/>

   <path id="classpath">
       <fileset dir="${env.GROOVY_HOME}/lib">
           <include name="**/*.jar"/>
       </fileset>
   </path>

   <taskdef name="groovy"
       classname="org.codehaus.groovy.ant.Groovy"
       classpathref="classpath"/>

   <target name="junit">
       <groovy>
            import org.junit.Test
            import org.junit.Assert

            println Test.protectionDomain.codeSource.location
            println Assert.protectionDomain.codeSource.location
       </groovy>
   </target>
</project>
------------------------------------------------------------------------------------

rgds,
Roshan

Jonathan Bernard

unread,
Feb 17, 2010, 12:12:40 PM2/17/10
to us...@groovy.codehaus.org
Thanks for taking the time to help. Problem solved.

The build file you supplied threw the same exception I was seeing
before. I removed the

import org.junit.Assert

println Assert.protectionDomain.codeSource.location

to see what I could find. The JUnit library was being loaded from the
ant classpath ($ANT_HOME/lib/junit4.jar) and it did not have the
Hamcrest libraries compiled in. The fix I chose was to update the
junit library in the ant lib folder.

Thanks,
Jonathan Bernard

Roshan Dawrani

unread,
Feb 17, 2010, 12:14:28 PM2/17/10
to us...@groovy.codehaus.org
Very good :-)
Reply all
Reply to author
Forward
0 new messages