JUnit - ANT 插件

2022-07-26 13:43 更新

在這個例子中,我們將展示如何使用 ANT 運行 JUnit。讓我們跟隨以下步驟:

步驟 1:下載 Apache Ant

下載 Apache ANT

操作系統(tǒng) 文件名
Windows apache-ant-1.8.4-bin.zip
Linux apache-ant-1.8.4-bin.tar.gz
Mac apache-ant-1.8.4-bin.tar.gz

步驟 2:設(shè)置 Ant 環(huán)境

設(shè)置 ANT_HOME 環(huán)境變量來指向 ANT 函數(shù)庫在機器中存儲的基本文件地址。例如,我們已經(jīng)在不同的操作系統(tǒng)的 apache-ant-1.8.4 文件夾中存儲了 ANT 函數(shù)庫。

操作系統(tǒng) 輸出
Windows 在 C:\Program Files\Apache Software Foundation
\apache-ant-1.8.4 中設(shè)置環(huán)境變量 ANT_HOME
Linux 導(dǎo)出 ANT_HOME=/usr/local/\apache-ant-1.8.4
Mac export ANT_HOME=/Library/\apache-ant-1.8.4

附加 ANT 編譯器地址到系統(tǒng)路徑,對于不同的操作系統(tǒng)來說如下所示:

操作系統(tǒng) 輸出
Windows 附加字符串 ;%ANT_HOME\bin to the end of the
system variable, Path.
Linux 導(dǎo)出 PATH=$PATH:$ANT_HOME/bin/
Mac 不需要

步驟 3:下載 Junit Archive

下載 JUnit Archive

操作系統(tǒng) 輸出
Windows junit4.10.jar
Linux junit4.10.jar
Mac junit4.10.jar

步驟 4:創(chuàng)建項目結(jié)構(gòu)

  • C:\ > JUNIT_WORKSPACE 中創(chuàng)建文件夾 TestJunitWithAnt
  • C:\ > JUNIT_WORKSPACE > TestJunitWithAnt 中創(chuàng)建文件夾 src
  • C:\ > JUNIT_WORKSPACE > TestJunitWithAnt 中創(chuàng)建文件夾 test
  • C:\ > JUNIT_WORKSPACE > TestJunitWithAnt 中創(chuàng)建文件夾 lib
  • C:\ > JUNIT_WORKSPACE > TestJunitWithAnt >src 文件夾中創(chuàng)建 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 文件夾中創(chuàng)建 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 文件夾中復(fù)制 junit-4.10.jar。

創(chuàng)建 ANT Build.xml

我們將使用 ANT 中的 任務(wù)來執(zhí)行我們的 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_WORKSPACE\TestJunitWithAnt>ant

驗證輸出。

Buildfile: C:\JUNIT_WORKSPACE\TestJunitWithAnt\build.xml

clean:  

compile:  
   [javac] Compiling 2 source files to C:\JUNIT_WORKSPACE\TestJunitWithAnt\test
   [javac] [parsing started C:\JUNIT_WORKSPACE\TestJunitWithAnt\src\
   MessageUtil.java]
   [javac] [parsing completed 18ms]
   [javac] [parsing started C:\JUNIT_WORKSPACE\TestJunitWithAnt\src\
   TestMessageUtil.java]
   [javac] [parsing completed 2ms]
   [javac] [search path for source files: C:\JUNIT_WORKSPACE\
   TestJunitWithAnt\src]    
   [javac] [loading java\lang\Object.class(java\lang:Object.class)]
   [javac] [loading java\lang\String.class(java\lang:String.class)]
   [javac] [loading org\junit\Test.class(org\junit:Test.class)]
   [javac] [loading org\junit\Ignore.class(org\junit:Ignore.class)]
   [javac] [loading org\junit\Assert.class(org\junit:Assert.class)]
   [javac] [loading java\lang\annotation\Retention.class
   (java\lang\annotation:Retention.class)]
   [javac] [loading java\lang\annotation\RetentionPolicy.class
   (java\lang\annotation:RetentionPolicy.class)]
   [javac] [loading java\lang\annotation\Target.class
   (java\lang\annotation:Target.class)]
   [javac] [loading java\lang\annotation\ElementType.class
   (java\lang\annotation:ElementType.class)]
   [javac] [loading java\lang\annotation\Annotation.class
   (java\lang\annotation:Annotation.class)]
   [javac] [checking MessageUtil]
   [javac] [loading java\lang\System.class(java\lang:System.class)]
   [javac] [loading java\io\PrintStream.class(java\io:PrintStream.class)]
   [javac] [loading java\io\FilterOutputStream.class
   (java\io:FilterOutputStream.class)]
   [javac] [loading java\io\OutputStream.class(java\io:OutputStream.class)]
   [javac] [loading java\lang\StringBuilder.class
   (java\lang:StringBuilder.class)]
   [javac] [loading java\lang\AbstractStringBuilder.class
   (java\lang:AbstractStringBuilder.class)]
   [javac] [loading java\lang\CharSequence.class(java\lang:CharSequence.class)]
   [javac] [loading java\io\Serializable.class(java\io:Serializable.class)]
   [javac] [loading java\lang\Comparable.class(java\lang:Comparable.class)]
   [javac] [loading java\lang\StringBuffer.class(java\lang:StringBuffer.class)]
   [javac] [wrote C:\JUNIT_WORKSPACE\TestJunitWithAnt\test\MessageUtil.class]
   [javac] [checking TestMessageUtil]
   [javac] [wrote C:\JUNIT_WORKSPACE\TestJunitWithAnt\test\TestMessageUtil.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


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號