SpringBoot項目單元測試以及jacoco生成單元測試覆蓋報告

概要:
我們在開發過程中通常都會編寫單元測試用例,而jacoco插件可以在我們打包項目前設置生成單元測試覆蓋報告,然後我們可以在瀏覽器中查看單元測試覆蓋率。
1、使用H2內存數據庫
首先要注意的是我們單元測試通常不要連接到數據庫中,比如測試一個插入數據的接口,如果連接數據庫會導致每次打包導致數據插入到數據庫中,破壞數據。
引入h2需要jar包:

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>

並且設置多個properties文件區分單測,研發,測試,生產環境。在單測文件中配置緩存數據庫

#單元測試配置
spring.datasource.driver-class-name=org.h2.Driver
#內存模式
spring.datasource.url=jdbc:h2:mem:test

在這裏插入圖片描述

2、配置jacoco插件

   <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.5.201505241946</version>
                <executions>
                    <execution>
                        <id>default-prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>default-report</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>default-check</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>check</goal>
                        </goals>
                        <configuration>
                            <rules>
                                <rule>
                                    <element>CLASS</element>
                                    <includes>
                                        <include>com.springboot.web.example.controller.*.*</include>
                                        <include>com.springboot.web.example.service.*.*</include>
                                    </includes>
                                    <limits>
                                        <limit>
                                            <counter>LINE</counter>
                                            <value>COVEREDRATIO</value>
                                            <minimum>0.00</minimum>
                                        </limit>
                                    </limits>
                                </rule>
                            </rules>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

3、控制檯輸入mvn clean package
在這裏插入圖片描述
在這裏插入圖片描述
4、拷貝報告文件路徑,瀏覽器打開
在這裏插入圖片描述
紅色表示未覆蓋,綠色表示覆蓋。通常覆蓋率要求達到80%左右,具體根據項目情況。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章