Maven test 生成Spring Boot 接口集成测试的中文测试报告

这里是一个对接口进行集成测试的例子。

1·测试类

package xxx.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import auth.AuthApplication;
import auth.model.users.ChangePwdVo;
import auth.model.users.UserSaveVo;
import auth.model.users.UserSearchVo;
import common.core.constant.SessionConstants;
import common.core.web.domain.IdListVo;
import common.core.web.domain.Result;
import common.core.web.page.Pager;
import org.junit.jupiter.api.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.*;

import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertTrue;


/**
 * 用户接口集成测试
 * @author zhout
 */
@DisplayName("用户接口集成测试")
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@SpringBootTest(classes = AuthApplication.class,
        webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class SysUserControllerIntegrationTest {


    protected Logger logger = LoggerFactory.getLogger(SysUserControllerIntegrationTest.class);

    @LocalServerPort
    private int port;

    @Autowired
    private TestRestTemplate restTemplate;

    @Autowired
    private ObjectMapper objectMapper;

    private static String userId;

    private final static String ADMIN_USER_ID = "62a16045adc5b0769dd55b8b";
    private final static String ADMIN_USER_PASSWPRD = "123456";

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUrl() {
        return "http://localhost:" + port + "/users/";
    }
    @BeforeAll
    public void setUp() {
        logger.info("用户接口集成测试 开始...");
    }

    @AfterAll
    public void tearDown() {
        logger.info("用户接口集成测试 结束...");
    }


    @DisplayName("新增用户测试")
    @Test
    @Order(1)
    void save() {
        UserSaveVo newUser = new UserSaveVo();
        newUser.setUn("testUser123");
        newUser.setP("z5ccc8wTqNnqvaISy/dOUw==");//qq123123
        newUser.setN("测试姓名");
        newUser.setG("男");
        newUser.setPh("19950088088");
        newUser.setM("[email protected]");
        newUser.setPi("职位");
        newUser.setNt("暂无备注");
        List<String> roles = new ArrayList<>();
        roles.add("62afeb37d116ad5d117c8814");
        newUser.setRi(roles);
        Result<UserSaveVo> response = this.restTemplate
                .postForObject(getUrl(), newUser, Result.class);
        assertTrue(200 == response.getCode(),response.getDesc());
        UserSaveVo saved = objectMapper.convertValue(response.getData(), UserSaveVo.class);
        setUserId(saved.getI());       
    }

    @DisplayName("修改用户测试")
    @Test
    @Order(2)
    void update() {
        UserSaveVo editUser = new UserSaveVo();
        editUser.setI(getUserId());
        editUser.setUn("testUser123");
        editUser.setN("测试姓名2");
        editUser.setG("男");
        editUser.setPh("19950088088");
        editUser.setM("[email protected]");
        editUser.setPi("职位");
        editUser.setNt("暂无备注");
        List<String> roles = new ArrayList<>();
        roles.add("62afeb37d116ad5d117c8814");
        editUser.setRi(roles);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<UserSaveVo> entity = new HttpEntity<>(editUser,headers);

        ResponseEntity<Result> response = this.restTemplate
                .exchange(getUrl(), HttpMethod.PUT, entity, Result.class);
        assertTrue(200 == response.getBody().getCode(),response.getBody().getDesc());

    }

    @DisplayName("验证用户密码测试")
    @Test
    @Order(4)
    void verifyPassword() {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set(SessionConstants.DETAILS_USER_ID,ADMIN_USER_ID);
        HttpEntity<UserSearchVo> entity = new HttpEntity<>(null,headers);

        ResponseEntity<Result> response = this.restTemplate.exchange(getUrl() + "/verifypassword/"+ADMIN_USER_PASSWPRD,
                HttpMethod.GET, entity,
                Result.class);
        assertTrue(200==response.getBody().getCode(),response.getBody().getDesc());
        assertTrue((Boolean) response.getBody().getData(),"校验密码失败");
    }

    @DisplayName("修改用户密码测试")
    @Test
    @Order(5)
    void changePassword() {
        ChangePwdVo changePwdVo = new ChangePwdVo();
        changePwdVo.setNp("5Bus/Fyl+OYiKfa/ejE3lw==");//qq123321
        changePwdVo.setOp("qq123123");
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set(SessionConstants.DETAILS_USER_ID,getUserId());
        HttpEntity<ChangePwdVo> entity = new HttpEntity<>(changePwdVo,headers);
        ResponseEntity<Result> response = this.restTemplate.exchange(getUrl()+"/password", HttpMethod.PUT, entity,
                Result.class);
        assertTrue(200 == response.getBody().getCode(),response.getBody().getDesc());
    }

    @DisplayName("重置用户密码测试")
    @Test
    @Order(6)
    void refreshPassword() {
        List<String> userIds = new ArrayList<>();
        userIds.add(getUserId());
        IdListVo idListVo = new IdListVo();
        idListVo.setIs(userIds);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<IdListVo> entity = new HttpEntity<>(idListVo,headers);
        ResponseEntity<Result> response = this.restTemplate.exchange(getUrl()+"/password/reset", HttpMethod.PUT, entity,
                Result.class);
        assertTrue(200 == response.getBody().getCode(),response.getBody().getDesc());
    }

    @DisplayName("删除用户测试")
    @Test
    @Order(8)
    void delete() {
        List<String> userIds = new ArrayList<>();
        userIds.add(getUserId());
        IdListVo idListVo = new IdListVo();
        idListVo.setIs(userIds);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<IdListVo> entity = new HttpEntity<>(idListVo,headers);
        ResponseEntity<Result> response = this.restTemplate.exchange(getUrl(), HttpMethod.DELETE, entity, Result.class);
        assertTrue(200 == response.getBody().getCode(),response.getBody().getDesc());
    }
}

2·POM.xml


<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">
  <parent>
    <artifactId>jxxartifactId>
    <groupId>xxgroupId>
    <version>1.0.0version>
  parent>
  <modelVersion>4.0.0modelVersion>

  <artifactId>xxxartifactId>

  <dependencies>

    
    <dependency>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-starter-testartifactId>
      <scope>testscope>
    dependency>

    
    <dependency>
      <groupId>org.junit.jupitergroupId>
      <artifactId>junit-jupiter-apiartifactId>
      <scope>testscope>
    dependency>
    <dependency>
      <groupId>org.junit.jupitergroupId>
      <artifactId>junit-jupiter-engineartifactId>
      <scope>testscope>
    dependency>
    <dependency>
      <groupId>org.junit.platformgroupId>
      <artifactId>junit-platform-launcherartifactId>
      <scope>testscope>
    dependency>

    
    <dependency>
      <groupId>com.alibaba.cloudgroupId>
      <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
    dependency>

    
    <dependency>
      <groupId>com.alibaba.cloudgroupId>
      <artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
    dependency>

    
    <dependency>
      <groupId>com.alibaba.cloudgroupId>
      <artifactId>spring-cloud-starter-alibaba-sentinelartifactId>
    dependency>

    
    <dependency>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-starter-webartifactId>
    dependency>

    
    <dependency>
      <groupId>io.springfoxgroupId>
      <artifactId>springfox-swagger-uiartifactId>
      <version>${swagger.fox.version}version>
    dependency>

    
    <dependency>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-starter-actuatorartifactId>
    dependency>

  dependencies>

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

      <plugin>
        <groupId>org.apache.maven.pluginsgroupId>
        <artifactId>maven-surefire-pluginartifactId>
        <version>3.0.0-M7version>
        <configuration>
          <testFailureIgnore>truetestFailureIgnore>
          <statelessTestsetReporter implementation="org.apache.maven.plugin.surefire.extensions.junit5.JUnit5Xml30StatelessReporter">
            <disable>falsedisable>
            <version>3.0version>
            <usePhrasedFileName>falseusePhrasedFileName>
            <usePhrasedTestSuiteClassName>trueusePhrasedTestSuiteClassName>
            <usePhrasedTestCaseClassName>trueusePhrasedTestCaseClassName>
            <usePhrasedTestCaseMethodName>trueusePhrasedTestCaseMethodName>
          statelessTestsetReporter>
          <consoleOutputReporter implementation="org.apache.maven.plugin.surefire.extensions.junit5.JUnit5ConsoleOutputReporter">
            <disable>falsedisable>
            <encoding>UTF-8encoding>
            <usePhrasedFileName>falseusePhrasedFileName>
          consoleOutputReporter>
          <statelessTestsetInfoReporter implementation="org.apache.maven.plugin.surefire.extensions.junit5.JUnit5StatelessTestsetInfoReporter">
            <disable>falsedisable>
            <usePhrasedFileName>falseusePhrasedFileName>
            <usePhrasedClassNameInRunning>trueusePhrasedClassNameInRunning>
            <usePhrasedClassNameInTestCaseSummary>trueusePhrasedClassNameInTestCaseSummary>
          statelessTestsetInfoReporter>
        configuration>
      plugin>
      <plugin>
        <groupId>org.apache.maven.pluginsgroupId>
        <artifactId>maven-site-pluginartifactId>
        <version>3.3version>
        <dependencies>
          <dependency>
            <groupId>org.apache.velocitygroupId>
            <artifactId>velocityartifactId>
            <version>1.5version>
          dependency>
        dependencies>
      plugin>

      <plugin>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-maven-pluginartifactId>
        <version>2.7.2version>
        <executions>
          <execution>
            <goals>
              <goal>repackagegoal>
            goals>
          execution>
        executions>
      plugin>

      <plugin>
        <groupId>org.apache.maven.pluginsgroupId>
        <artifactId>maven-surefire-report-pluginartifactId>
        <version>3.0.0-M7version>
        <executions>
          <execution>
            <id>reportid>
            <phase>testphase>
            <goals>
              <goal>reportgoal>
            goals>
          execution>
        executions>
      plugin>

    plugins>
  build>


  <reporting>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.pluginsgroupId>
        <artifactId>maven-surefire-report-pluginartifactId>
        <version>3.0.0-M7version>
        <configuration>
          <outputName>xx-Test-ReportoutputName>
          <outputDirectory>${basedir}/target/surefire-reportsoutputDirectory>
        configuration>
      plugin>
    plugins>
  reporting>
project>

3·执行mvn test 就会生成测试报告,报告位于target\site\surefire-report.html
Maven test 生成Spring Boot 接口集成测试的中文测试报告_第1张图片

你可能感兴趣的:(Java,spring,maven,java,集成测试)