编写主代码
public class HelloWorld {
public String sayHello() {
return "Hello Maven!";
}
}
测试代码
pom添加单元测试依赖:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.pengxiaohe</groupId>
<artifactId>maven-test</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
上述pom代码中有一个值为test的元素scope,scope为依赖范围,若依赖范围为test则表示该依赖只对测试有效,换句话说,测试代码中import Junit代码是没有问题的,但是如果在主代码中用import Junit代码,就会造成编译错误。如果不声明依赖范围,默认值是compile,表示依赖对主代码和测试代码都有效。
测试类:
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class TestHelloWorld {
@Test
public void testSayHello() {
HelloWorld helloWorld = new HelloWorld();
String sayHelloRes = helloWorld.sayHello();
assertEquals("Hello Maven!", sayHelloRes);
}
}
一个典型的单元测试包含三个步骤:
- 准备测试类和测试数据
- 执行要测试的行为;
- 检查结果
执行测试
mvn clean test
执行结果:
[INFO] Scanning for projects…
[INFO]
[INFO] ————————————————————————
[INFO] Building maven-test 1.0-SNAPSHOT
[INFO] ————————————————————————
[INFO]
[INFO] — maven-clean-plugin:2.5:clean (default-clean) @ maven-test —
[INFO] Deleting /Users/pengxiaohe/private_project/common/maven-test/target
[INFO]
[INFO] — maven-resources-plugin:2.6:resources (default-resources) @ maven-test —
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] — maven-compiler-plugin:3.1:compile (default-compile) @ maven-test —
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /Users/pengxiaohe/private_project/common/maven-test/target/classes
[INFO]
[INFO] — maven-resources-plugin:2.6:testResources (default-testResources) @ maven-test —
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/pengxiaohe/private_project/common/maven-test/src/test/resources
[INFO]
[INFO] — maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven-test —
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /Users/pengxiaohe/private_project/common/maven-test/target/test-classes
[INFO]
[INFO] — maven-surefire-plugin:2.12.4:test (default-test) @ maven-test —[INFO] Surefire report directory: /Users/pengxiaohe/private_project/common/maven-test/target/surefire-reports
T E S T S
——————————————————- Running com.pizi.helloworld.TestHelloWorld Tests run: 1, Failures: 0, Errors:
0, Skipped: 0, Time elapsed: 0.06 secResults :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
———————————————————————— [INFO] BUILD SUCCESS [INFO]
———————————————————————— [INFO] Total time: 1.558 s [INFO] Finished at:2018-03-03T23:53:11+08:00 [INFO] Final Memory: 17M/306M [INFO]
Maven在执行测试之前,它会先执行项目的主资源管理,主代码编译,测试资源管理、测试代码编译等工作,这是maven生命周期的一个特性。