Search code examples
javajunitantproguardhamcrest

Error when running JUnit: Fixing in build.xml


I am inheriting a project. I have recently updated Proguard and JUnit versions and am having a problem running the command ant jtest.

Versions of software I currently have:

Java 1.8.0_141

Ant 1.10.1

Proguard: recently updated to 5.3.3, was 4.9

JUnit: recently updated to 4.12, was 4.10

The test suite runs fine in Eclipse, but when running from Cygwin with the command I get this error at the end of the output:

jtest:
[mkdir] Created dir: C:\users\user\Documents\Workspace\project\build\test-data
 [echo]
[junit] Testsuite: com.package.project.AllTests
[junit] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.016 sec
[junit]
[junit] Testcase: initializationError took 0.002 sec
[junit]     Caused an ERROR
[junit] org/hamcrest/SelfDescribing
[junit] java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
[junit]     at java.lang.ClassLoader.defineClass1(Native Method)
[junit]     at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
[junit]     at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
[junit]     at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
[junit]     at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
[junit]     at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
[junit]     at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
[junit]     at java.security.AccessController.doPrivileged(Native Method)
[junit]     at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
[junit]     at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
[junit]     at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
[junit]     at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[junit]     at java.lang.Class.getDeclaredConstructors0(Native Method)
[junit]     at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)
[junit]     at java.lang.Class.getConstructor0(Class.java:3075)
[junit]     at java.lang.Class.getConstructor(Class.java:1825)
[junit]     at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
[junit] Caused by: java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing
[junit]     at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
[junit]     at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
[junit]     at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
[junit]     at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[junit]

Here is my build.xml file

I am not very familiar with build.xml files. I understand that this error is because JUnit has a dependency of hamcrest-core-1.3.jar. How can I point JUnit at the hamcrest-core-1.3.jar file in order to make this command work? Can I do it from build.xml?


Solution

  • Your jtest target runs junit with this classpath:

    <path id="test.classpath">
        <pathelement location="${testclassesdir}" />
        <pathelement location="${builddir}/project.jar" />
        <path refid="project.libs" />
        <pathelement location="${toolsdir}/junit4.12/junit-4.12.jar" />
    </path>
    

    You need to add hamcrest-core-1.3.jar to that classpath. You can do this by creating a directory name "hamcrest1.3" to your ${toolsdir} and downloading hamcrest-core-1.3.jar into that directory and then updating the test.classpath to add this pathelement:

    <pathelement location="${toolsdir}/hamcrest1.3/hamcrest-core-1.3.jar" />