文章目录
加载测试专用配置
有效解决配置冲突问题
案例演示
编写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)]](https://code84.com/wp-content/uploads/2022/10/ef40b23988f84624832214a17f304274.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sbif4UEd-1653141947063)(springboot.assets/image-20220521182414552.png)]](https://code84.com/wp-content/uploads/2022/10/e3e68ee486af4bd3a7910aab34765717.png)
测试类中启动web环境
只需要一个webEnvironment类就能让我们的测试类启动起来web环境
进而可以让我们进行web相关接口的测试
案例演示
编写xml
starter后面加个web,刷新maven即可
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-A47HJxon-1653141947063)(springboot.assets/image-20220521182910704.png)]](https://code84.com/wp-content/uploads/2022/10/cfe6003242ee4342bf7865cab6f1e55b.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)]](https://code84.com/wp-content/uploads/2022/10/e112c49986fe409d9874d0aeb9939e58.png)
小结
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-S8tAaRbb-1653141947065)(springboot.assets/image-20220521210833700.png)]](https://code84.com/wp-content/uploads/2022/10/0acde52cfa534f3cb44ef298e501a5c8.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)]](https://code84.com/wp-content/uploads/2022/10/f7e2a35e6ebb4632975451e072471198.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)]](https://code84.com/wp-content/uploads/2022/10/5861581f100d4c5bbc948602e44b841b.png)
小结
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VudjnWg3-1653141947068)(springboot.assets/image-20220521220446436.png)]](https://code84.com/wp-content/uploads/2022/10/817a7532545947eab614299ff7d67527.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-urVvGgDX-1653141947069)(springboot.assets/image-20220521220502452.png)]](https://code84.com/wp-content/uploads/2022/10/91c4a9a65435497baa42a41a172ac9a8.png)
版权声明:本文为qq_39123467原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。