Java一个 SpringBoot 单元测试示例

Posted 张学徒

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java一个 SpringBoot 单元测试示例相关的知识,希望对你有一定的参考价值。

一个 SpringBoot 单元测试示例

学习自:springboot(16)Spring Boot使用单元测试

jdk 1.8

pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>ValidAnnotationTest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>ValidAnnotationTest</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.6.4</version>
            </plugin>
        </plugins>
    </build>

</project>

TestController.java

package com.example.validannotationtest.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.constraints.NotNull;


@Controller
public class TestController 

    @PostMapping(value = "/test")
    @ResponseBody
    public String updateLearn(@RequestBody String learn)
        return learn;
    


ValidAnnotationTestApplicationTests.java

package com.example.validannotationtest;

import com.example.validannotationtest.entity.User;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;


@SpringBootTest
class ValidAnnotationTestApplicationTests 

    @Autowired
    private WebApplicationContext wac;

    private MockMvc mvc;
    private MockHttpSession session;

    // 需要是 @BeforeEach 才生效,junit 5.x中,@Before主键被@BeforeEach所替代
    // > 引用自:https://blog.csdn.net/weixin_45612794/article/details/105450942
    @BeforeEach
    public void setupMockMvc() 
        //初始化MockMvc对象
        mvc = MockMvcBuilders.webAppContextSetup(wac).build();
        session = new MockHttpSession();
    

    @Test
    void test() throws Exception 

        String json = "\\"author\\":\\"ZhangSan\\",\\"title\\":\\"Spring\\",\\"url\\":\\"http://test.top/\\"";
        mvc.perform(MockMvcRequestBuilders.post("/test")
                .accept(MediaType.APPLICATION_JSON_UTF8)
                .content(json.getBytes())   // 传 JSON 参数
                .session(session)
        )
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andDo(MockMvcResultHandlers.print());
    


文件结构

运行后的结果

以上是关于Java一个 SpringBoot 单元测试示例的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot单元测试示例2

java~springboot~h2数据库在单元测试中的使用

ABAP和Java SpringBoot的单元测试

在SpringBoot中集成单元测试

在SpringBoot中集成单元测试

Java教程:Springboot项目如何使用Test单元测试