Ant 新手,JUnit 的 ClassNotFoundException [英] New to Ant, ClassNotFoundException with JUnit

查看:27
本文介绍了Ant 新手,JUnit 的 ClassNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为此挠头一段时间了(谷歌搜索了一堆,浏览了其他相关的 SO 帖子,但无济于事).我有一个由两个文件组成的 Java 程序,LogicTests.Tests 包含大约一百个 JUnit 测试,我通过调用 javac *.javajava org.junit 获得了 100% 的成功率.runner.JUnitCore 测试.但是,当我使用简单的 ant -verbose test 运行我的 build.xml 时(为了遵循输出,因为我对这一切都不熟悉),我得到以下内容输出:

I've been scratching my head over this for a while now (Googled a bunch, looked through other related SO posts to no avail). I have a Java program comprised of two files, Logic and Tests. Tests contains about a hundred JUnit tests, and I've gotten 100% success rate with said tests by calling javac *.java followed by java org.junit.runner.JUnitCore Tests. However when I run my build.xml with a simple ant -verbose test (in order to follow the output since I'm new to all this), I get the following output:

[junit] Testsuite: Tests
[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
[junit] 
[junit] Null Test:  Caused an ERROR
[junit] Tests
[junit] java.lang.ClassNotFoundException: Tests
[junit]     at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
[junit]     at java.lang.Class.forName0(Native Method)
[junit]     at java.lang.Class.forName(Class.java:247)
[junit] 
[junit] 
[junit] Test Tests FAILED

BUILD SUCCESSFUL

我的build.xml如下:

<project name="ETL_Automation" default="test" basedir=".">

<path id="classpath.base">
</path>

<path id="classpath.test">
  <pathelement location="${basedir}/mysql-connector-java-5.1.18-bin.jar" />
  <pathelement location="${basedir}/junit-4.10.jar"/>
  <path refid="classpath.base" />
</path>

<target name="compile">
  <javac srcdir="${basedir}">
    <classpath refid="classpath.test"/>
  </javac>
</target>

<target name="test" depends="compile">
  <junit fork="no">
    <classpath refid="classpath.test" />
    <formatter type="brief" usefile="false" />
    <batchtest>
      <fileset dir="${basedir}/" includes="Tests.class" />
    </batchtest>
  </junit>
</target>

<target name="clean" depends="test">
  <delete>
    <fileset dir="${basedir}" includes="*.class"/>
  </delete>
</target>

目录结构非常简单.Tests.javaLogic.javajunit-4.10.jarmysql-connector-java-5.1.18-bin.jarbuild.xml 和引用的 .properties 文件都在同一个文件夹中.Java 代码引用了外部文件,但这些文件与此特定问题无关.我不知道类路径是否可能是导致此问题的原因(因为我非常确信我目前拥有的方法不起作用).

The directory structure is pretty straightforward. Tests.java, Logic.java, junit-4.10.jar, mysql-connector-java-5.1.18-bin.jar, build.xml, and a referenced .properties file are all in the same folder. The java code references external files but those are unrelated to this particular issue. I don't know if the classpath could be the cause of this issue (as I'm pretty convinced what I currently have doesn't work).

谢谢!

推荐答案

您需要将带有 Tests.class 的目录添加到 classpath.tests 类路径(这是您设置中的 ${basedir})

You will need to add the directory with the Tests.class to the classpath.tests classpath (which is ${basedir} in your setup)

试试:

<path id="classpath.test"> 
  <pathelement location="${basedir}/mysql-connector-java-5.1.18-bin.jar" /> 
  <pathelement location="${basedir}/junit-4.10.jar"/> 
  <pathelement location="${basedir}" /> 
  <path refid="classpath.base" /> 
</path>

这篇关于Ant 新手,JUnit 的 ClassNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆