文章目录
pom.xml 的配置(注意事项,非常重要)
- pom.xml 的配置(注意事项,非常重要)
- 测试案例
- 执行测试命令
- surefire 插件配置
1.必须引入 maven-surefire-plugin 插件,否则无法使用 Maven 的测试功能
2.maven-surefire-plugin 插件只支持 junit-jupiter-api 构件,不支持 junit 构件
所以在 pom.xml 文件关于测试的配置内容如下:
<dependencies> <dependency> <groupId>org.junit.jupiter <plugin> <groupId>org.apache.maven.plugins public int add(int a, int b) { return a + b; } public int subtract(int a, int b) { return a - b; } }
接着在 src/test/java 目录下创建测试用例(即用于测试的类),代码如下:
package com.example.demo02; import org.junit.Assert; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; /** * description * * @author liaowenxiong * @date 2022/1/28 08:18 */ public class HelloMavenTest { private HelloMaven hm; @BeforeEach public void setUp() { hm = new HelloMaven(); } @Test public void testAdd() throws InterruptedException { int a = 1; int b = 2; int result = hm.add(a, b); Assert.assertEquals(a + b, result); } @Test public void testSubtract() throws InterruptedException { int a = 1; int b = 2; int result = hm.subtract(a, b); Assert.assertEquals(a - b, result); } @AfterEach public void tearDown() throws Exception { System.out.println("测试结束了!"); } }执行测试命令
测试用例写好之后,就要执行测试的命令,可以通过以下几种方式执行测试的命令。
第一种:命令终端
打开命令终端,切换到 pom.xml 所在目录下,执行命令 mvn test。执行命令 mvn test,表示执行到构件生命周期的 test 阶段,之前的阶段都会自动执行。执行 mvn test 命令的过程如下:
[~/Documents/IdeaProjects/struts2-demo01]$ mvn test [INFO] Scanning for projects... [INFO] [INFO] ------------------< priv.lwx.struts2:struts2-demo01 >------------------- [INFO] Building struts2-demo01 Maven Webapp 1.0-SNAPSHOT [INFO] --------------------------------[ war ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ struts2-demo01 --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 2 resources [INFO] [INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ struts2-demo01 --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ struts2-demo01 --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory /Users/liaowenxiong/Documents/IdeaProjects/struts2-demo01/src/test/resources [INFO] [INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ struts2-demo01 --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ struts2-demo01 --- [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running priv.lwx.struts2.util.ConnectionUtilsTest 配置文件路径:/Users/liaowenxiong/Documents/IdeaProjects/struts2-demo01/target/classes/db_config.properties Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary. com.mysql.cj.jdbc.ConnectionImpl@221a3fa4 [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.954 s - in priv.lwx.struts2.util.ConnectionUtilsTest [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 4.345 s [INFO] Finished at: 2022-02-07T13:00:50+08:00 [I;NFO] ------------------------------------------------------------------------
第二种:Maven 操作窗口
选择生命周期的 test 阶段,点击上面的绿色三角图标。
第三种:Maven Goal 的窗口中执行
第四种:IDEA内置的命令终端窗口
这里要特别注意,如果只是执行命令 mvn surefire:test,那么之前的生命周期阶段是不会自动执行的,也就是说主代码不会被构建,测试代码也不会被构建,而是执行测试的指令,因为执行命令 mvn surefire:test 表示只执行 surefire 插件的 test 目标而已,执行命令过程如下:
[~/Documents/IdeaProjects/struts2-demo01]$ mvn surefire:test [INFO] Scanning for projects... [INFO] [INFO] ------------------< priv.lwx.struts2:struts2-demo01 >------------------- [INFO] Building struts2-demo01 Maven Webapp 1.0-SNAPSHOT [INFO] --------------------------------[ war ]--------------------------------- [INFO] [INFO] --- maven-surefire-plugin:2.22.1:test (default-cli) @ struts2-demo01 --- [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running priv.lwx.struts2.util.ConnectionUtilsTest 配置文件路径:/Users/liaowenxiong/Documents/IdeaProjects/struts2-demo01/target/classes/db_config.properties Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary. com.mysql.cj.jdbc.ConnectionImpl@221a3fa4 [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.855 s - in priv.lwx.struts2.util.ConnectionUtilsTest [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.477 s [INFO] Finished at: 2022-02-07T12:54:54+08:00 [INFO] ------------------------------------------------------------------------surefire 插件配置
<plugin> <groupId>org.apache.maven.plugins <exclude>**/TestConstants.java <forkMode>always关注打赏