JUnit - Ant插件

JUnit - Ant插件 首页 / JUnit入门教程 / JUnit - Ant插件

无涯教程将通过一个示例演示如何使用ANT运行JUnit。请遵循以下步骤。

步骤1 - 下载Apache Ant

根据您正在使用的操作系统,下载 Apache Ant

步骤2 - 设定Ant环境

将 ANT_HOME 环境变量设置为指向基本目录位置,该目录在您的计算机上存储ANT库。假设Ant库存储在apache-ant-1.8.4文件夹中。

链接:https://www.learnfk.comhttps://www.learnfk.com/junit/junit-plug-with-ant.html

来源:LearnFk无涯教程网

Sr.No.OS & Remark
1

Windows

将环境变量 ANT_HOME设置为C:\Program Files\Apache Software Foundation\apache-ant-1.8.4

2

Linux

Export ANT_HOME=/usr/local/apache-ant-1.8.4

无涯教程网

3

Mac

Export ANT_HOME=/Library/apache-ant-1.8.4

将Ant编译器位置附加到系统路径,如下所示-

OS输出
Windows在系统变量 Path 的末尾附加字符串%ANT_HOME\bin 。
LinuxExport PATH=$PATH:$ANT_HOME/bin/
Mac不需要

步骤3 - 下载JUnit包

下载适合您的操作系统的JUnit存档。

OSJar名称
Windows junit4.10.jar
Linux junit4.10.jar
Mac junit4.10.jar

步骤4 - 创建项目结构

  • 创建测试文件目录C:\>JUNIT_WORKSPACE>TestJunitWithAnt> src。

  • 在C:\> JUNIT_WORKSPACE> TestJunitWithAnt> src 文件夹中创建MessageUtil类。

/*
* 此类在控制台上打印给定的消息。
*/
public class MessageUtil {

   private String message;

   //构造函数
   //@param message to be printed
   public MessageUtil(String message){
      this.message = message; 
   }

   //打印消息
   public String printMessage(){
      System.out.println(message);
      return message;
   }   

   //添加“Hi!”到消息
   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());
   }
}

将junit-4.10.jar复制到文件夹C:\> JUNIT_WORKSPACE> TestJunitWithAnt> lib。

创建ANT Build.xml

无涯教程将在Ant中使用<junit>任务执行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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

数据结构与算法之美 -〔王争〕

全栈工程师修炼指南 -〔熊燚(四火)〕

Java业务开发常见错误100例 -〔朱晔〕

爆款文案修炼手册 -〔乐剑峰〕

如何落地业务建模 -〔徐昊〕

手把手带你写一门编程语言 -〔宫文学〕

朱涛 · Kotlin编程第一课 -〔朱涛〕

Kubernetes入门实战课 -〔罗剑锋〕

云时代的JVM原理与实战 -〔康杨〕

好记忆不如烂笔头。留下您的足迹吧 :)