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

SpringBootMvc测试-基于SpringBoot2.5.7

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

SpringBootMvc测试-基于SpringBoot2.5.7

SpringBoot Mvc测试常用注解

一般SpringBoot Mvc 测试,需要配合@SpringBootTest来一起使用。需要注意的是,SpringBootTest注解默认提供一个mock web env(一个轻量级web server),这个mock server 有些限制,比如servlet container 底层行为、自定义error page等行为是不支持的。官方说明如下:

AutoConfigureMockMvc注解

提供MockMvc、WebTestClient两种方式来模拟调用http请求

注解说明
MockMvc只支持serlvet 容器mvc
WebTestClient只支持webFlux,当classpath 有spring webflux jar时,会自动实例化WebTestClient

注解属性说明

属性说明
addFilters是否添加filter 到web 容器中,默认为true
print打印MvcResult模式是哪种,默认为MockMvcPrint.SYSTEM_OUT

关键实现class 见SpringBootMockMvcBuilderCustomizer

注解源码
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@documented
@Inherited
@importAutoConfiguration
@PropertyMapping("spring.test.mockmvc")
public @interface AutoConfigureMockMvc {

	
	boolean addFilters() default true;

	
	@PropertyMapping(skip = SkipPropertyMapping.ON_DEFAULT_VALUE)
	MockMvcPrint print() default MockMvcPrint.DEFAULT;

	
	boolean printOnlyOnFailure() default true;

	
	@PropertyMapping("webclient.enabled")
	boolean webClientEnabled() default true;

	
	@PropertyMapping("webdriver.enabled")
	boolean webDriverEnabled() default true;

}
AutoConfigureWebTestClient注解

只提供WebTestClient方式来模拟调用http请求
注解属性说明

属性说明
timeout超时时间
注解源码
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@documented
@Inherited
@importAutoConfiguration
@PropertyMapping("spring.test.webtestclient")
public @interface AutoConfigureWebTestClient {

	
	String timeout() default "";

}
demo 代码
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.reactive.server.WebTestClient;
import org.springframework.test.web.servlet.MockMvc;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringBootTest
@AutoConfigureMockMvc(addFilters = false, printonlyOnFailure = true)
public class SpringBootMvcDemoTest {
    @Test
    void mvcMockTest(@Autowired MockMvc mvc) throws Exception {
        mvc.perform(get("/")).andExpect(status().isOk()).andExpect(content().string("hello world"));
    }

    @Test
    void webClientTest(@Autowired WebTestClient client) throws Exception {
        client.get().uri("/").exchange().expectStatus().isOk()
                .expectBody(String.class).isEqualTo("Hello World");
    }
}

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

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

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