运行JUnit时出错:修复build.xml

时间:2017-09-26 20:30:40

标签: java junit ant proguard hamcrest

我正在继承一个项目。我最近更新了Proguard和JUnit版本,并且在运行命令ant jtest时遇到问题。

我目前拥有的软件版本:

Java 1.8.0_141

Ant 1.10.1

Proguard:最近更新到5.3.3,为4.9

JUnit:最近更新到4.12,是4.10

测试套件在Eclipse中运行良好,但是当使用命令从Cygwin运行时,我在输出结束时遇到此错误:

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]

这是我的build.xml文件

我对build.xml文件不太熟悉。我理解这个错误是因为JUnit具有hamcrest-core-1.3.jar的依赖性。如何将jUnit指向hamcrest-core-1.3.jar文件以使该命令有效?我可以从build.xml做到吗?

1 个答案:

答案 0 :(得分:1)

您的jtest目标使用此类路径运行junit

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

您需要将hamcrest-core-1.3.jar添加到该类路径中。您可以通过创建目录名称来实现此目的&#34; hamcrest1.3&#34;到您的${toolsdir}downloading hamcrest-core-1.3.jar进入该目录,然后更新test.classpath以添加此pathelement

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