ant 1.9.2 + junit ..类未找到错误

时间:2013-10-26 08:32:05

标签: ant junit

我正在使用JDK 1.7,eclipse 4.2.2,JUnit 4.8.1,ant 1.9.2,windows8 64位**当我运行build.xml时,所有目标都运行正常,但名称为运行运行不正常。

的build.xml

<?xml version="1.0" encoding="UTF-8"?>
  <project name="AntExample" default="setclasspath" basedir=".">
    <!-- Properties Initialization -->
    <property environment="env"/> 
    <property name="ws.home" value="${basedir}" />
    <property name="ws.jar" value="${basedir}/lib" />
    <property name="test.dest" value="${basedir}/build" />
    <property name="test.src" value="${basedir}/src" />
    <property name="test.reportsDir" value="E:\Reports" />

    <!-- Path Initialization -->
    <path id="testcase.path">
      <pathelement location="${ws.jar}"/>
      <pathelement path="${classes}"/>
      <fileset dir="${ws.jar}">
        <include name="**/*.jar"/>
      </fileset>
    </path>

    <!-- Target Setclasspath -->
    <target name="setclasspath" unless="testcase.path">
      <path id="classpath_jars">
        <fileset dir="${ws.jar}">
          <include name="**/*.jar"/>
        </fileset>
      </path>
      <pathconvert pathsep=";" property="testcase.path" refid="classpath_jars"/>
    </target>

    <!-- Target init -->
    <target name="init" depends="setclasspath">
      <condition property="ANT"
                 value="${env.ANT_HOME}/bin/ant.bat"
                 else="${env.ANT_HOME}/bin/ant">
        <os family="windows"/>
      </condition>
    </target>

    <!-- Target clean -->
    <target name="clean">
      <delete dir="${test.dest}"/>
    </target>

    <!-- Target compile -->
    <target name="compile" depends="init,clean">
      <delete includeemptydirs="true" quiet="true">
        <fileset dir="${test.dest}">
          <include name="**/*"/>
        </fileset>
      </delete>
      <mkdir dir="${test.dest}"/>
      <javac debug="true"
             destdir="${test.dest}"
             srcdir="${test.src}"
             target="1.7"
             classpath="${testcase.path}"/>
    </target>

    <!-- Target run -->
    <target name="run" >
      <delete includeemptydirs="true" quiet="true">
        <fileset dir="${test.reportsDir}">
          <include name="**/*"/>
        </fileset>
      </delete> 
      <mkdir dir="${test.reportsDir}"/>
      <java jar="${ws.jar}"  fork="yes" spawn="yes"/>
      <junit fork="yes" haltonfailure="no" printsummary="yes" >
        <classpath refid="testcase.path" />
        <batchtest todir="${test.reportsDir}" fork="true">
          <fileset dir="${test.dest}">
            <include name="anttestcase/Login.class"/>
          </fileset>
        </batchtest>
        <formatter type="xml"/>
      </junit>

      <junitreport todir="${test.reportsDir}">
        <fileset dir="${test.reportsDir}">
          <include name="TESTS-*.xml"/>
        </fileset>
        <report todir="${test.reportsDir}"/>
      </junitreport>
    </target>
  </project>`

当我使用命令提示符运行我的构建时,我收到以下错误:

C:\Users\Ashish Rathore\workspace\AntExample>ant run
  

Buildfile:C:\ Users \ Ashish Rathore \ workspace \ AntExample \ build.xml

     

运行:       [junit]警告:在junit [junit]的路径中检测到多个版本的ant   罐子:文件:/ H:/ant1.9/apache-ant-1.9.2-bin/apache-ant-1.9.2/lib/ant.jar /org/apache/tools/ant/Project.class   [junit的]和   罐子:文件:/ C:/Users/Ashish%20Rathore/workspace/AntExample/lib/ant.jar /org/apache/tools/ant/Project.class   [junit]运行anttestcase.Login [junit]测试运行:1,失败:0,   错误:1,跳过:0,经过的时间:0秒[junit]测试   anttestcase.Login FAILED [junitreport]处理   E:\ Reports \ TESTS-TestSuites.xml到C:\ Users \ ASHISH~1 \ Ap   pData \ Local \ Temp \ null1158960870 [junitreport]加载样式表   罐子:文件:/ H:/ant1.9/apache-ant-1.9.2-bin/apache   -ant-1.9.2 / lib目录/ ANT-的junit.jar!/组织/阿帕奇/工具/蚂蚁/任务定义/可选/ JUnit的/ XSL / J   unit-frames.xsl [junitreport]转换时间:1223ms [junitreport]   正在删除:C:\ Users \ ASHISH~1 \ AppData \ Local \ Temp \ null1158960870

     

建立成功总时间:5秒

我的报告中的TEST-anttestcase.Login.xml中的错误消息

error type="java.lang.ClassNotFoundException" message="anttestcase.Login">
 java.lang.ClassNotFoundException: anttestcase.Login at
   java.net.URLClassLoader$1.run(URLClassLoader.java:366) at
   java.net.URLClassLoader$1.run(URLClassLoader.java:355) at
   java.security.AccessController.doPrivileged(Native Method) at
   java.net.URLClassLoader.findClass(URLClassLoader.java:354) at
   java.lang.ClassLoader.loadClass(ClassLoader.java:423) at
   sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at
   java.lang.ClassLoader.loadClass(ClassLoader.java:356) at
   java.lang.Class.forName0(Native Method) at
   java.lang.Class.forName(Class.java:188)

我的Login.java测试用例

package anttestcase;

import junit.framework.Assert;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ErrorCollector;

public class Login {
  @Rule public ErrorCollector errCollector=new ErrorCollector();

  @BeforeClass public static void setUp() throws Exception {
    System.out.println("opening Url");
  }

  @AfterClass public static void tearDown() throws Exception {
  }

  @Test public void enterCredentials() {
    try {
      Assert.assertEquals("A", "B");
      System.out.println("Enter Username and Password");
    } catch(Throwable t) {
      errCollector.addError(t);   
      System.out.println("Caught");
    }
  }

  @Test public void authenticityCheck() {
    System.out.println("Login Successfully");
  }
}

1 个答案:

答案 0 :(得分:4)

您必须将已编译的类添加到junit任务的类路径中。

    <junit fork="yes" haltonfailure="no" printsummary="yes" >
    <classpath>
      <path refid="testcase.path">
      <pathelement location="${test.dest}"/>
    </classpath>
    <batchtest todir="${test.reportsDir}" fork="true">
      <fileset dir="${test.dest}">
        <include name="anttestcase/Login.class"/>
      </fileset>
    </batchtest>
    <formatter type="xml"/>
  </junit>

在Github上有一个示例ant项目,它使用JUnit:https://github.com/mplacona/java-junit-template-project看看它build.xml