SpringBoot测试类启动web环境

1.坐标修改

XML 复制代码
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

2.测试类测试

说明:@SpringBootTest()中的webEnvironment值的说明;

2.1不启动

说明:默认不启动,如下图所示。@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)。

java 复制代码
package com.forever;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
public class WebTest {
    @Test
    void test(){

    }

}

2.2默认端口

说明:@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)

java 复制代码
package com.forever;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class WebTest {
    @Test
    void test(){

    }

}

2.3 随机端口

说明:随机端口,@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)。

java 复制代码
package com.forever;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class WebTest {
    @Test
    void test(){

    }

}

3.发送虚拟请求

3.1@AutoConfigureMockMvc

说明:开启虚拟mvc调用。

java 复制代码
package com.forever;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//开启虚拟mvc调用
@AutoConfigureMockMvc
public class WebTest {

    @Test
    //形参写入或者上方写@Autowired;为方法注入资源。
    void testWeb(@Autowired MockMvc mockMvc) throws Exception {
        //发请求http://....../users;下面就是模拟的一个http请求,访问的是users
        MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/users");
        //执行对应的请求。
        mockMvc.perform(builder);

    }


}

3.2成功

相关推荐
HeLiang71 分钟前
proguard 混淆 使用JDK17 的 springboot4 + JPA
java·spring boot·proguard
武子康3 分钟前
Java-10 深入浅出 MyBatis 一对多与多对多查询配置详解
java·后端
一 乐3 分钟前
网上订餐系统|基于springboot的网上订餐系统设计与实现(源码+数据库+文档)
java·数据库·spring boot·后端·论文·毕设·网上订餐系统
摇滚侠4 分钟前
我把一个依赖安装到了本地仓库,但是IDEA 刷新 maven 提示远程私服仓库找不到,怎么解决
java·maven·intellij-idea
.Cnn8 分钟前
SpringBoot 文件上传与阿里云 OSS 集成
java·spring boot·后端·阿里云
之歆10 分钟前
Day21_电商详情页核心技术实战:从LESS预处理到复杂交互实现
开发语言·前端·javascript·css·交互·less
Mininglamp_271810 分钟前
现在入局Agent开发还来得及吗?
java·开发语言
疯狂成瘾者13 分钟前
GHCR 是什么?GitHub 容器镜像仓库技术介绍
java·linux
方也_arkling17 分钟前
【Java-Day10】多态
java·开发语言
海鸥两三19 分钟前
基于 Vue 3 + 高德地图的网格规划系统实战(有源码)
前端·javascript·vue.js