Maven如何进行单元测试呢?

乔欣 Maven教程 发布时间:2023-02-08 08:27:20 阅读数:14792 1
下文笔者讲述maven进行单元测试的方法及示例分享,如下所示

maven单元测试的简介说明

maven单元测试可使用 mvn test进行,也可以只对指定类进行测试
  如:
$ mvn test
 
# Run a single test class.
$ mvn -Dtest=TestApp1 test
 
# Run multiple test classes.
$ mvn -Dtest=TestApp1,TestApp2 test
 
# Run a single test method from a test class.
$ mvn -Dtest=TestApp1#methodname test
 
# Run all test methods that match pattern 'testFun*' from a test class.
$ mvn -Dtest=TestApp1#testFun* test
 
# Run all test methods match pattern 'testFun*' and 'testCarModel*' from a test class.
$ mvn -Dtest=TestApp1#testFun*+testCarModel* test

注意事项:
    使用以上的模式匹配测试,需在pom.xml引入相应的依赖
  pom.xml
<build>
        <plugins>
 
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.0</version>
            </plugin>
 
        </plugins>
    </build>
例:
创建一个Maven和JUnit5的测试样例
pom.xml定义

<?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>com.java265.examples</groupId>
    <artifactId>maven-unit-test</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
 
    <properties>
        <!-- https://maven.apache.org/general.html#encoding-warning -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
 
    <dependencies>
 
        <!-- junit 5, unit test -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.3.1</version>
            <scope>test</scope>
        </dependency>
 
    </dependencies>
    <build>
        <finalName>maven-unit-test</finalName>
        <plugins>
 
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.0</version>
            </plugin>
 
        </plugins>
    </build>
 
</project>


//MagicBuilder.java

package com.java265.examples; 
public class MagicBuilder {
 
    public static int getLucky() {
        return 8888888;
    } 
}

//MessageBuilder.java

package com.java265.examples;
public class MessageBuilder {
    public static String getHelloWorld(){
        return "hello Java爱好者";
    }
 
    public static int getNumber100(){
        return 100;
    }
 
}

//MagicBuilder测试类TestMagicBuilder.java

package com.java265.examples;
 
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class TestMagicBuilder {
 
    @Test
    public void testLucky() {
        assertEquals(8888888, MagicBuilder.getLucky());
    }
 
}

//1.5 MessageBuilder测试类
 
package com.java265.examples;
 
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class TestMessageBuilder {
 
    @Test
    public void testFunWorld() {
        assertEquals("hello Java爱好者", MessageBuilder.getHelloWorld());
    }
 
    @Test
    public void testNumber100() {
        assertEquals(100, MessageBuilder.getNumber100());
    }
 
}

Maven运行测试

//运行所有测试类
$ mvn test

//运行单个测试类TestMessageBuilder
$ mvn -Dtest=TestMessageBuilder test
 
//运行测试类TestMessageBuilder运行单个测试方法testFunWorld()
 
$ mvn -Dtest=TestMessageBuilder#testFunWorld test
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/Maven/202302/5706.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者