源和测试“源”目录 - 如何从Test from Source运行测试

时间:2013-09-24 21:15:46

标签: ant junit

我有一个Java项目,它有一个'src'目录,用于与我的项目直接相关的代码,以及一个'test'目录,它是针对'src'目录中的代码运行的JUnit测试。目录结构如下:

ROOT/src/com/mycompany/decoders/qDecoder/.....

ROOT/test/com/mycompany/decoders/qDecoder/.....

两个目录都具有完全相同的包结构。

以下是目标和相关的JUnit信息:

<property name="src_classes_dir" value="./bin"/>
<property name="test_classes_dir" value="./binTest"/>

<path id="junit.classpath">
  <pathelement location="${src_classes_dir}"/>
  <pathelement location="${test_classes_dir}"/>
  <pathelement location="${CommonJarPath}/JUnit/junit.jar"/>
  <pathelement location="${CommonJarPath}/JavaMail/mailapi.jar"/>
  <pathelement location="${CommonJarPath}/JavaMail/smtp.jar"/>
  <pathelement location="${CommonJarPath}/CommonsLang3/commons-lang3.jar"/>
  <pathelement location="${CommonJarPath}/JDBC/inetsoftware/merlia/8.0.2/Merlia.jar"/>
  <pathelement location="${CommonJarPath}/JodaTime/joda-time.jar"/>
</path>

<property name="test_dir" value="./test"/>  

<target name="junit" description="Run JUnit tests" depends="compile_source, compile_test">        
  <junit fork="yes" haltonfailure="yes" haltonerror="yes" showoutput="yes">
    <formatter type="plain" usefile="no"/>
    <classpath refid="junit.classpath"/>                
    <batchtest>         
      <fileset dir="${test_dir}" />
    </batchtest>    
  </junit>
</target>

因此,我的测试文件(*.java)位于${test_dir}。测试类位于${test_classes_dir}中,测试类位于${src_classes_dir}中。两者都列在junit.classpath参考中。

所有目标都已完成,'clean','prep','src_compile'和'test_compile'直到'junit'目标,然后我在第一次测试时得到ClassNotFoundException {{ 1}}。所以它知道测试,但由于某种原因找不到它的类。

这是异常追踪:

AMCiBitsTest

我很难过。我究竟做错了什么?为什么不能运行junit: [junit] Testsuite: com.amci.util.AMCiBitsTest [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec [junit] [junit] Caused an ERROR [junit] com.amci.util.AMCiBitsTest [junit] java.lang.ClassNotFoundException: com.amci.util.AMCiBitsTest [junit] at java.net.URLClassLoader$1.run(URLClassLoader.java:366) [junit] at java.net.URLClassLoader$1.run(URLClassLoader.java:355) [junit] at java.security.AccessController.doPrivileged(Native Method) [junit] at java.net.URLClassLoader.findClass(URLClassLoader.java:354) [junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:424) [junit] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) [junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:357) [junit] at java.lang.Class.forName0(Native Method) [junit] at java.lang.Class.forName(Class.java:190) [junit] 测试和所有其他测试?

1 个答案:

答案 0 :(得分:0)

我已经修好了。我只想说我在构建文件中出现了语法错误,上面发布的内容实际上是可行的。对此表示歉意,感谢您的问题和时间。