testng + mockito + spring boot test 基本操作

潼关路边的一只野鬼發表於2024-06-22

程式碼地址

https://gitee.com/bzrj/thresh-boot

如何使用

  1. thresh-dependencies 目錄執行 mvn clean install
  2. 在跟目錄執行 make

效果

jacoco

image

image

allure

image

image

關鍵配置

thresh-test

  1. 此模組包含了測試需要的依賴
  2. 定義了兩個 testng 監聽

thresh-report

此模組專門用於聚合 jacoco 和 allure 的報告

<?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>
    <parent>
        <groupId>com.laolang.thresh</groupId>
        <artifactId>thresh-boot</artifactId>
        <version>${revision}</version>
    </parent>

    <artifactId>thresh-report</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.laolang.thresh</groupId>
            <artifactId>thresh-module-system-biz</artifactId>
        </dependency>
        <dependency>
            <groupId>com.laolang.thresh</groupId>
            <artifactId>thresh-module-auth-biz</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- 生成聚合報告 -->
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>report</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report-aggregate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

模組 pom

注意:
由於使用了 powermock , 所以需要使用 jacoco 的 offline 模式

    <build>
        <plugins>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>${jacoco-maven-plugin.version}</version>
                <executions>
                    <execution>
                        <id>default-instrument</id>
                        <goals>
                            <goal>instrument</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>default-restore-instrumented-classes</id>
                        <goals>
                            <goal>restore-instrumented-classes</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>pre-test</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>post-test</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                        <configuration>
                            <dataFile>${project.build.directory}/jacoco.exec</dataFile>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven-surefire-plugin.version}</version>
                <configuration>
                    <argLine>
                        -Dfile.encoding=UTF-8
                        -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
                    </argLine>
                    <testFailureIgnore>true</testFailureIgnore>
                    <systemPropertyVariables>
                        <jacoco-agent.destfile>${project.build.directory}/jacoco.exec</jacoco-agent.destfile>
                    </systemPropertyVariables>
                    <!--生成allure-result的目錄-->
                    <systemProperties>
                        <!--是否忽略html,解釋見下圖。與之後在reportNg報告上顯示截圖相關。當前已經使用allure了,這裡可以直接去掉啦-->
                        <!--<org.uncommons.reportng.escape-output>false</org.uncommons.reportng.escape-output>-->
                        <!--定義輸出在專案 target 目錄-->
                        <property>
                            <name>allure.results.directory</name>
                            <value>target/allure-results</value>
                        </property>
                    </systemProperties>
                    <suiteXmlFiles>
                        <!--該檔案位於工程根目錄時,直接填寫名字,其它位置要加上路徑-->
                        <suiteXmlFile>${xmlFileName}</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
        </plugins>
    </build>

根目錄

用於配置外掛依賴

    <build>
        <finalName>${project.artifactId}</finalName>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin.version}</version>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
        </plugins>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <version>${jacoco-maven-plugin.version}</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>${maven-surefire-plugin.version}</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

相關文章