栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

文章目录
  • 加载测试专用配置
    • 案例演示
      • 编写ConfigrationTest.java
      • 编写MsgConfig.java
    • 小结
  • 测试类中启动web环境
    • 案例演示
      • 编写xml
      • 编写WebTest.java
      • 测试运行
    • 小结
  • 发送虚拟请求
    • 案例模拟
      • 新建UserController.java
      • 运行测试
    • 案例模拟2
      • 编写WebTest.java
      • 测试运行
    • 小结

加载测试专用配置

有效解决配置冲突问题

案例演示 编写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;


@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;


@SuppressWarnings({"all"})
@Configuration
public class MsgConfig {
    @Bean
    public String msg(){
        return "bean msg";
    }
}
小结

测试类中启动web环境

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

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

案例演示 编写xml

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

编写WebTest.java
package com.taotao;

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


@SuppressWarnings({"all"})
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) //随机端口
public class WebTest {
    @Test
    void test(){

    }
}

测试运行

小结

发送虚拟请求 案例模拟 新建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;


@SuppressWarnings({"all"})
@RestController
@RequestMapping("/Users")
public class UserController {
    @GetMapping
    public String getById(){
        System.out.println("getById is running");
        return "springboot";
    }
}
运行测试

案例模拟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;


@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);
    }
}
测试运行

小结

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/927319.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号