SpringBoot开发实用篇 - 测试 - 加载测试专用配置,测试类中启动web环境,发送虚拟请求模拟

加载测试专用配置

有效解决配置冲突问题

案例演示

编写ConfigrationTest.java

package com.taotao;

import com.taotao.config.MsgConfig;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;

/**
 * create by 刘鸿涛
 * 2022/5/21 18:20
 */
@SuppressWarnings({"all"})
@SpringBootTest
@Import({MsgConfig.class})
public class ConfigrationTest {
    @Autowired
    private String msg;

    @Test
    void testConfiguration(){
        System.out.println(msg);
    }
}

编写MsgConfig.java

package com.taotao.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * create by 刘鸿涛
 * 2022/5/21 18:19
 */
@SuppressWarnings({"all"})
@Configuration
public class MsgConfig {
    @Bean
    public String msg(){
        return "bean msg";
    }
}

小结

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kSYnfdMO-1653141947062)(springboot.assets/image-20220521182349205.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sbif4UEd-1653141947063)(springboot.assets/image-20220521182414552.png)]

测试类中启动web环境

只需要一个webEnvironment类就能让我们的测试类启动起来web环境

进而可以让我们进行web相关接口的测试

案例演示

编写xml

starter后面加个web,刷新maven即可

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-A47HJxon-1653141947063)(springboot.assets/image-20220521182910704.png)]

编写WebTest.java

package com.taotao;

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

/**
 * create by 刘鸿涛
 * 2022/5/21 19:41
 */
@SuppressWarnings({"all"})
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) //随机端口
public class WebTest {
    @Test
    void test(){

    }
}

测试运行

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-J0xLdYT1-1653141947064)(springboot.assets/image-20220521205035763.png)]

小结

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-S8tAaRbb-1653141947065)(springboot.assets/image-20220521210833700.png)]

发送虚拟请求

案例模拟

新建UserController.java

package com.taotao.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * create by 刘鸿涛
 * 2022/5/21 21:29
 */
@SuppressWarnings({"all"})
@RestController
@RequestMapping("/Users")
public class UserController {
    @GetMapping
    public String getById(){
        System.out.println("getById is running");
        return "springboot";
    }
}

运行测试

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hQIjAWvt-1653141947066)(springboot.assets/image-20220521213522952.png)]

案例模拟2

编写WebTest.java

package com.taotao;

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.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

/**
 * create by 刘鸿涛
 * 2022/5/21 19:41
 */
@SuppressWarnings({"all"})
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) //随机端口
@AutoConfigureMockMvc
public class WebTest {
    @Test
    void test(){

    }

    @Test
    void testWeb(@Autowired MockMvc mvc) throws Exception {
        //创建虚拟请求,当前访问/Users
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/Users");
        //执行对应的请求
        mvc.perform(builder);
    }
}

测试运行

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1l7OuyMu-1653141947068)(springboot.assets/image-20220521220317351.png)]

小结

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VudjnWg3-1653141947068)(springboot.assets/image-20220521220446436.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-urVvGgDX-1653141947069)(springboot.assets/image-20220521220502452.png)]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

鬼鬼骑士

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

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

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

打赏作者

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

抵扣说明:

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

余额充值