JUnit – ANT 插件

JUnit – ANT 插件

在这个例子中,我们将展示如何使用 ANT 运行 JUnit。让我们跟随以下步骤:

步骤 1:下载 Apache Ant

下载 Apache ANT

操作系统文件名
Windowsapache-ant-1.8.4-bin.zip
Linuxapache-ant-1.8.4-bin.tar.gz
Macapache-ant-1.8.4-bin.tar.gz

步骤 2:设置 Ant 环境

设置 ANT_HOME 环境变量来指向 ANT 函数库在机器中存储的基本文件地址。例如,我们已经在不同的操作系统的 apache-ant-1.8.4 文件夹中存储了 ANT 函数库。

操作系统输出
Windows在 C:Program FilesApache Software Foundation
apache-ant-1.8.4 中设置环境变量 ANT_HOME
Linux导出 ANT_HOME=/usr/local/apache-ant-1.8.4
Macexport ANT_HOME=/Library/apache-ant-1.8.4

附加 ANT 编译器地址到系统路径,对于不同的操作系统来说如下所示:

操作系统输出
Windows附加字符串 ;%ANT_HOMEin to the end of the
system variable, Path.
Linux导出 PATH=$PATH:$ANT_HOME/bin/
Mac不需要

步骤 3:下载 Junit Archive

下载 JUnit Archive

操作系统输出
Windowsjunit4.10.jar
Linuxjunit4.10.jar
Macjunit4.10.jar

步骤 4:创建项目结构

  • C: > JUNIT_WORKSPACE 中创建文件夹 TestJunitWithAnt
  • C: > JUNIT_WORKSPACE > TestJunitWithAnt 中创建文件夹 src
  • C: > JUNIT_WORKSPACE > TestJunitWithAnt 中创建文件夹 test
  • C: > JUNIT_WORKSPACE > TestJunitWithAnt 中创建文件夹 lib
  • C: > JUNIT_WORKSPACE > TestJunitWithAnt >src 文件夹中创建 MessageUtil 类
/*
* This class prints the given message on console.
*/
public class MessageUtil {

   private String message;

   //Constructor
   //@param message to be printed
   public MessageUtil(String message){
      this.message = message; 
   }

   // prints the message
   public String printMessage(){
      System.out.println(message);
      return message;
   }   

   // add "Hi!" to the message
   public String salutationMessage(){
      message = "Hi!" + message;
      System.out.println(message);
      return message;
   }   
}
  • C: > JUNIT_WORKSPACE > TestJunitWithAnt > src 文件夹中创建 TestMessageUtil 类。
import org.junit.Test;
import org.junit.Ignore;
import static org.junit.Assert.assertEquals;

public class TestMessageUtil {

   String message = "Robert";   
   MessageUtil messageUtil = new MessageUtil(message);

   @Test
   public void testPrintMessage() { 
      System.out.println("Inside testPrintMessage()");     
      assertEquals(message,messageUtil.printMessage());
   }

   @Test
   public void testSalutationMessage() {
      System.out.println("Inside testSalutationMessage()");
      message = "Hi!" + "Robert";
      assertEquals(message,messageUtil.salutationMessage());
   }
}
  • C: > JUNIT_WORKSPACE > TestJunitWithAnt > lib 文件夹中复制 junit-4.10.jar。

创建 ANT Build.xml

我们将使用 ANT 中的 任务来执行我们的 junit 测试样例。

<project name="JunitTest" default="test" basedir=".">
   <property name="testdir" location="test" />
   <property name="srcdir" location="src" />
   <property name="full-compile" value="true" />
   <path id="classpath.base"/>
   <path id="classpath.test">
      <pathelement location="/lib/junit-4.10.jar" />
      <pathelement location="${testdir}" />
      <pathelement location="${srcdir}" />
      <path refid="classpath.base" />
   </path>
   <target name="clean" >
      <delete verbose="${full-compile}">
         <fileset dir="${testdir}" includes="**/*.class" />
      </delete>
   </target>
   <target name="compile" depends="clean">
      <javac srcdir="${srcdir}" destdir="${testdir}" 
         verbose="${full-compile}">
         <classpath refid="classpath.test"/>
      </javac>
   </target>
   <target name="test" depends="compile">
      <junit>
         <classpath refid="classpath.test" />
         <formatter type="brief" usefile="false" />
         <test name="TestMessageUtil" />
      </junit>
   </target>
</project>

运行下列的 ant 命令

C:JUNIT_WORKSPACETestJunitWithAnt>ant

验证输出。

Buildfile: C:JUNIT_WORKSPACETestJunitWithAntuild.xml

clean:  

compile:  
   [javac] Compiling 2 source files to C:JUNIT_WORKSPACETestJunitWithAnt	est
   [javac] [parsing started C:JUNIT_WORKSPACETestJunitWithAntsrc
   MessageUtil.java]
   [javac] [parsing completed 18ms]
   [javac] [parsing started C:JUNIT_WORKSPACETestJunitWithAntsrc
   TestMessageUtil.java]
   [javac] [parsing completed 2ms]
   [javac] [search path for source files: C:JUNIT_WORKSPACE
   TestJunitWithAntsrc]    
   [javac] [loading javalangObject.class(javalang:Object.class)]
   [javac] [loading javalangString.class(javalang:String.class)]
   [javac] [loading orgjunitTest.class(orgjunit:Test.class)]
   [javac] [loading orgjunitIgnore.class(orgjunit:Ignore.class)]
   [javac] [loading orgjunitAssert.class(orgjunit:Assert.class)]
   [javac] [loading javalangannotationRetention.class
   (javalangannotation:Retention.class)]
   [javac] [loading javalangannotationRetentionPolicy.class
   (javalangannotation:RetentionPolicy.class)]
   [javac] [loading javalangannotationTarget.class
   (javalangannotation:Target.class)]
   [javac] [loading javalangannotationElementType.class
   (javalangannotation:ElementType.class)]
   [javac] [loading javalangannotationAnnotation.class
   (javalangannotation:Annotation.class)]
   [javac] [checking MessageUtil]
   [javac] [loading javalangSystem.class(javalang:System.class)]
   [javac] [loading javaioPrintStream.class(javaio:PrintStream.class)]
   [javac] [loading javaioFilterOutputStream.class
   (javaio:FilterOutputStream.class)]
   [javac] [loading javaioOutputStream.class(javaio:OutputStream.class)]
   [javac] [loading javalangStringBuilder.class
   (javalang:StringBuilder.class)]
   [javac] [loading javalangAbstractStringBuilder.class
   (javalang:AbstractStringBuilder.class)]
   [javac] [loading javalangCharSequence.class(javalang:CharSequence.class)]
   [javac] [loading javaioSerializable.class(javaio:Serializable.class)]
   [javac] [loading javalangComparable.class(javalang:Comparable.class)]
   [javac] [loading javalangStringBuffer.class(javalang:StringBuffer.class)]
   [javac] [wrote C:JUNIT_WORKSPACETestJunitWithAnt	estMessageUtil.class]
   [javac] [checking TestMessageUtil]
   [javac] [wrote C:JUNIT_WORKSPACETestJunitWithAnt	estTestMessageUtil.class]
   [javac] [total 281ms]

test:
    [junit] Testsuite: TestMessageUtil
    [junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 0.008 sec
    [junit]
    [junit] ------------- Standard Output ---------------
    [junit] Inside testPrintMessage()
    [junit] Robert
    [junit] Inside testSalutationMessage()
    [junit] Hi!Robert
    [junit] ------------- ---------------- ---------------

BUILD SUCCESSFUL
Total time: 0 seconds

作者:唐伯虎点蚊香,如若转载,请注明出处:https://www.web176.com/junit/16689.html

(0)
打赏 支付宝 支付宝 微信 微信
唐伯虎点蚊香的头像唐伯虎点蚊香
上一篇 2023年4月24日
下一篇 2023年4月24日

相关推荐

发表回复

登录后才能评论