1

JDK 1.7、Eclipse 4.2.2、JUnit 4.8.1、ant 1.9.2、windows8 64 ビットを使用しています ** build.xml を実行すると、すべてのターゲットは正常に動作しますが、名前がrunのターゲットは正常に動作しません.

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

ビルドファイル: C:\Users\Ashish Rathore\workspace\AntExample\build.xml

実行: [junit] 警告: 複数のバージョンの ant が junit [junit] のパスで検出されました
jar:ファイル:/H:/ant1.9/apache-ant-1.9.2-bin/apache-ant-1.9.2/lib/ant.jar!/org/apache/tools/ant/Project.class [junit ]and jar:file:/C:/Users/Ashish%20Rathore/workspace/AntExample/lib/ant.jar!/org/apache/tools/ant/Project.class [junit]Running anttestcase.Login [junit]Tests run : 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] スタイルシート jar のロード:file:/H:/ant1.9/apache-ant-1.9.2-bin/apache -ant-1.9.2/lib/ant-junit .jar!/org/apache/tools/ant/taskdefs/optional/junit/xsl/j unit-frames.xsl [junitreport] 変換時間: 1223ms [junitreport] 削除: C:\Users\ASHISH~1\AppData\Local \Temp\null1158960870

BUILD SUCCESSFUL 合計時間: 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");
  }
}
4

1 に答える 1

5

コンパイルされたクラスを 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 には、JUnit を使用する ant プロジェクトの例があります: https://github.com/mplacona/java-junit-template-projectをご覧くださいbuild.xml

于 2013-10-26T12:35:06.273 に答える