13.Springboot整合junit5单元测试与生成单元测试覆盖率

现在基本大公司都要求单元测试了,保证我们代码得质量,而我司更是要求覆盖率要达到60%以上,所以搞一下。

1.maven集成

<!-- 单元测试覆盖率 -->
<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-test</artifactId>
 	 <version>2.7.2</version>
 </dependency>
<!-- junit5 -->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.8.2</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-runner</artifactId>
    <version>1.7.0</version>
    <scope>test</scope>
</dependency>

2.maven单元测试覆盖率集成组件

<!-- 跳过测试test -->
<build>
<plugins>
<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-surefire-plugin</artifactId>
   <version>2.22.2</version>
   <configuration>
       <!-- skipTests设置为false才可以走单元测试哦。如果设置为true的话,要么不会生成覆盖率,要么覆盖率为  -->
       <skipTests>false</skipTests>
       <includes>
           <include>**/Test*.java</include>
           <include>**/*Test.java</include>
           <include>**/*Tests.java</include>
           <include>**/*TestCase.java</include>
           <include>**/*Spec.class</include>
       </includes>
   </configuration>
</plugin>
 <plugin>
      <groupId>org.jacoco</groupId>
           <artifactId>jacoco-maven-plugin</artifactId>
           <version>0.8.7</version>
           <executions>
               <execution>
                   <id>prepare-agent</id>
                   <goals>
                       <goal>prepare-agent</goal>
                   </goals>
               </execution>
               <execution>
                   <id>report</id>
                   <phase>test</phase>
                   <goals>
                       <goal>report</goal>
                   </goals>
               </execution>
           </executions>
</plugin> 

</plugins>
</build>

3.编写单元测试

创建单测启动基类

import org.springframework.boot.test.context.SpringBootTest;

/**
 * @author cf
 * @date 2022/12/26下午 2:52
 */
@SpringBootTest(classes = BasicPatternApplication.class)
public class BaseTest {
}

resources里面得配置文件要迁移到test文件夹下,如下图:

在这里插入图片描述

编写单测类:


import com.qax.needle.framework.boot.model.PageResult;
import org.junit.jupiter.api.Test;
import org.junit.Assert;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletResponse;

/**
 * @author cf
 * @date 2022/12/26下午 2:51
 */
public class AreasAccessInfoServiceImplTest extends BaseTest {

    @Autowired
    IAreasAccessInfoService iAreasAccessInfoService;
    private MockHttpServletResponse response;

    @Test
    public void page() {
        AreasAccessInfoPageReq areasAccessInfo = new AreasAccessInfoPageReq();
        Integer offset = 0;
        Integer limit = 10;
        areasAccessInfo.setCheckpointArea("110101");
        areasAccessInfo.setCheckpointName("检");
        areasAccessInfo.setLineType("1");
        areasAccessInfo.setCheckTimeEnd(1671465600000L);
        areasAccessInfo.setCheckTimeStart(1669824000000L);
        PageResult<AreasAccessInfoPageResp> page = iAreasAccessInfoService.page(areasAccessInfo, offset, limit);
        Assert.assertNotNull(page);
    }

    @Test
    public void statistics() {
        AreasAccessInfoPageReq areasAccessInfo = new AreasAccessInfoPageReq();
        areasAccessInfo.setCheckpointArea("110101");
        areasAccessInfo.setCheckpointName("");
        areasAccessInfo.setLineType("1");
        areasAccessInfo.setCheckTimeEnd(1671465600000L);
        areasAccessInfo.setCheckTimeStart(1669824000000L);
        AreasAccessInfoStatisticsResp statistics = iAreasAccessInfoService.statistics(areasAccessInfo);
        Assert.assertNotNull(statistics);
    }

    @Test
    public void export() {
        AreasAccessInfoPageReq areasAccessInfo = new AreasAccessInfoPageReq();
        areasAccessInfo.setCheckpointArea("110101");
        areasAccessInfo.setCheckpointName("检");
        areasAccessInfo.setLineType("1");
        areasAccessInfo.setCheckTimeEnd(1671465600000L);
        areasAccessInfo.setCheckTimeStart(1669824000000L);
        response = new MockHttpServletResponse();
        iAreasAccessInfoService.export(areasAccessInfo, response);
        Assert.assertNotNull("1");
    }
}

4.执行单元测试覆盖率

这里有两个方法:
1.使用maven自带得test,idea右侧maven模块执行项目下得test
2.使用cmd命令,在你的项目pom文件所在目录 ,打开cmd,执行如下:

mvn clean test org.jacoco:jacoco-maven-plugin:0.8.7:prepare-agent install -Dmaven.test.failure.ignore=true

结果如下:打开site文件夹下得 index.html
在这里插入图片描述
出来了:
在这里插入图片描述

5.有个坑

在编写单测得时候注意不要引入@Test得包为org.junit.Test,因为

  • spring boot 2.2之前使用的是 Junit4
  • spring boot 2.2之后使用的是 Junit5
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
为在Spring Boot中进行JUnit5单元测试,你需要在测试类上使用@SpringBootTest注解。这个注解会加载整个Spring应用程序上下文,以便你可以使用Spring的功能和特性。一个简单的示例是这样的: ```java @SpringBootTest class MyUnitTest { // 测试方法 @Test void myTest() { // 测试逻辑 } } ``` 在测试方法中,你可以使用@Autowired注解来注入依赖的bean,并使用@Transactional注解标记方法以支持事务管理。@Transactional注解可以确保在测试完成后自动回滚,以保持测试的独立性。 与JUnit4相比,JUnit5Spring Boot 2.2.0版本及以上的默认单元测试库。因此,如果你使用的是最新版本的Spring Boot,你应该使用JUnit5来编写你的单元测试。 在系统环境方面,你需要确保你的Java版本是8及以上,Maven版本是3.3及以上,以及使用IDEA作为开发工具。当你创建一个Maven项目时,你需要导入spring-boot-starter-test依赖来使用单元测试的场景。如果你使用的是Spring Initializr创建的项目,它将自动引入单元测试的依赖,你不需要手动添加。 这是使用JUnit5进行Spring Boot单元测试的一般步骤。希望对你有帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [JUnit5单元测试(基于SpringBoot)](https://blog.csdn.net/maogenb/article/details/124459093)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞四海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值