在这个例子中,我们将展示如何使用 ANT 运行 JUnit。让我们跟随以下步骤:
下载 Apache ANT
操作系统 | 文件名 |
---|---|
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 |
设置 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 |
Mac | export ANT_HOME=/Library/apache-ant-1.8.4 |
附加 ANT 编译器地址到系统路径,对于不同的操作系统来说如下所示:
操作系统 | 输出 |
---|---|
Windows | 附加字符串 ;%ANT_HOMEbin to the end of the
system variable, Path. |
Linux | 导出 PATH=$PATH:$ANT_HOME/bin/ |
Mac | 不需要 |
下载 JUnit Archive
操作系统 | 输出 |
---|---|
Windows | junit4.10.jar |
Linux | junit4.10.jar |
Mac | junit4.10.jar |
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;
}
}
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());
}
}
我们将使用 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_WORKSPACETestJunitWithAntbuild.xml
clean:
compile:
[javac] Compiling 2 source files to C:JUNIT_WORKSPACETestJunitWithAnttest
[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_WORKSPACETestJunitWithAnttestMessageUtil.class]
[javac] [checking TestMessageUtil]
[javac] [wrote C:JUNIT_WORKSPACETestJunitWithAnttestTestMessageUtil.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
摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢!“简单,踏实~ 读书写字放屁”一、为何用RESTful A...
正如你在前面一节看到的,在简单的场景下,MyBatis可以为你自动映射查询结果。但如果遇到复杂的场景,你需要构建一个结果映射。...
script要在带注解的映射器接口类中使用动态SQL,可以使用script元素。比如:@Update({"script","update Author","set","if te...
SqlSessionFactorySqlSessionFactory有六个方法创建SqlSession实例。通常来说,当你选择其中一个方法时,你需要...
拦截器你已经学到,在 Hibernate 中,一个对象将被创建和保持。一旦对象已经被修改,它必须被保存到数据库里。这个过程持续直到...