java springboot 初体验 (十)对接ut

  1. 上一篇
    1. java springboot 初体验 (九)对接http请求
    2. https://www.cnblogs.com/zwjvzwj/p/16614205.html
  2. pom文件添加依赖包

    1. <!-- 自动查找主类 用于打包 和 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.8.1</version>
      <executions>
      <execution>
      <id>prepare-agent</id>
      <goals>
      <goal>prepare-agent</goal>
      </goals>
      </execution>
      <execution>
      <id>report</id>
      <phase>prepare-package</phase>
      <goals>
      <goal>report</goal>
      </goals>
      </execution>
      <execution>
      <id>post-unit-test</id>
      <phase>test</phase>
      <goals>
      <goal>report</goal>
      </goals>
      <configuration>
      <dataFile>target/jacoco.exec</dataFile>
      </configuration>
      </execution>
      </executions>
      </plugin>
      </plugins>
      </build>

       

    2.  

       

       

  3.   在test中添加测试文件
    1. package com.zwj.zwjproject.testController.outer;
      
      import com.zwj.zwjproject.ZwjProjectApplication;
      import com.zwj.zwjproject.controller.outer.TestOuterController;
      import com.zwj.zwjproject.dto.TestApiReqDTO;
      import com.zwj.zwjproject.dto.TestApiRespDTO;
      import com.zwj.zwjproject.result.RespEntity;
      import org.junit.jupiter.api.Test;
      import org.springframework.boot.test.context.SpringBootTest;
      
      import javax.annotation.Resource;
      
      /**
       * @ClassName: testOuterController
       * @Author zhangwujie
       * @Date 2022/8/23 3:12 下午
       * @Description:
       */
      @SpringBootTest(classes = ZwjProjectApplication.class)
      public class TestTestOuterController {
      
          @Resource
          private TestOuterController testOuterController;
      
          @Test
          public void testtestApi() {
      
              try {
                  String s = testOuterController.testApi();
                  assert "success".equals(s);
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      }

       

    2.  
  4.   运行jacoco
    1.   主要用来生成单元测试报告
    2.  直接点击idea的install或者使用命令"mvn install" 、"mvn jacoco:report" //生成报告
    3.  点击test运行单元测试
  5. 下一篇
    1. java springboot 初体验 (十一)对接mysql
    2. https://www.cnblogs.com/zwjvzwj/p/16620045.html
posted @ 2022-08-23 20:38  zwjvzwj  阅读(107)  评论(0编辑  收藏  举报