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 Files\Apache 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_HOME\bin 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 类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
* 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 类。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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 测试样例。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<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验证输出。