因此,在编写测试用例之前,我们先创建该目录。
在Java世界中,由Kent Beck和Erich Gamma建立的JUnit是事实上的单元测试标准。
要使用JUnit,我们首先需要为Hello World项目添加一个JUnit依赖,修改项目的POM如代码清单3-3:
代码清单3-3:为Hello World的POM添加依赖
Java代码
< xml version="1.0" encoding="UTF-8" >
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v 4_0_0.xsd">
代码中添加了dependencies元素,该元素下可以包含多个dependency元素以声明项目的依赖,
这里我们添加了一个依赖——groupId是junit,artifactId是junit,version是4.7。
前面我们提到groupId、artifactId和version是任何一个Maven项目最基本的坐标,
JUnit也不例外,有了这段声明,Maven就能够自动下载junit-4.7.jar。
也许你会问,Maven从哪里下载这个jar呢?在Maven之前,我们可以去JUnit的官网下载分发包。
而现在有了Maven,它会自动访问中央仓库(http://repo1.maven.org/maven2/),下载需要的文件。
读者也可以自己访问该仓库,打开路径junit/junit/4.7/,就能看到junit-4.7.pom和junit-4.7.jar。
上述POM代码中还有一个值为test的元素scope,scope为依赖范围,
若依赖范围为test则表示该依赖只对测试有效,换句话说,测试代码中的import JUnit代码是没有问题的,
但是如果我们在主代码中用import JUnit代码,就会造成编译错误。
如果不声明依赖范围,那么默认值就是compile,表示该依赖对主代码和测试代码都有效。
配置了测试依赖,接着就可以编写测试类,
回顾一下前面的HelloWorld类,现在我们要测试该类的sayHello()方法,
检查其返回值是否为“Hello Maven”。在src/test/java目录下创建文件,
其内容如代码清单3-4:
代码清单3-4:Hello World的测试代码
Java代码
package com.juvenxu.mvnbook.helloworld;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class HelloWorldTest
{
@Test
public void testSayHello()
{
HelloWorld helloWorld = new HelloWorld();
String result = helloWorld.sayHello();
assertEquals( "Hello Maven", result );
}
}
一个典型的单元测试包含三个步骤:
一,准备测试类及数据;
二,执行要测试的行为;
三,检查结果。
上述样例中,我们首先初始化了一个要测试的HelloWorld实例,
接着执行该实例的sayHello()方法并保存结果到result变量中,
最后使用JUnit框架的Assert类检查结果是否为我们期望的”Hello Maven”。
在JUnit 3中,约定所有需要执行测试的方法都以test开头,这里我们使用了JUnit 4,
在JUnit 4中,需要执行的测试方法都应该以@Test进行标注。
测试用例编写完毕之后就可以调用Maven执行测试,运行 mvn clean test :
Java代码
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Hello World Project
[INFO] task-segment: [clean, test]
[INFO] ------------------------------------------------------------------------
[INFO] [clean:clean {execution: default-clean}]
[INFO] Deleting directory D:\git-juven\mvnbook\code\hello-world\target
[INFO] [resources:resources {execution: default-resources}]
Downloading: http://repo1.maven.org/maven2/junit/junit/4.7/junit-4.7.pom
1K downloaded (junit-4.7.pom)
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Compiling 1 source file to D: \code\hello-world\target\classes
[INFO] [resources:testResources {execution: default-testResources}]
Downloading: http://repo1.maven.org/maven2/junit/junit/4.7/junit-4.7.jar
226K downloaded (junit-4.7.jar)
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] Compiling 1 source file to D:\ code\hello-world\target\test-classes
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Compilation failure
D:\code\hello-world\src\test\java\com\juvenxu\mvnbook\helloworld\HelloWorldTest.java:[8,5] -source 1.3 中不支持注释
(请使用 -source 5 或更高版本以启用注释)
@Test
[INFO] ----------------------------------------------