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 {

	/**
	 * If filters from the application context should be registered with MockMVC. Defaults
	 * to {@code true}.
	 * @return if filters should be added
	 */
	boolean addFilters() default true;

	/**
	 * How {@link MvcResult} information should be printed after each MockMVC invocation.
	 * @return how information is printed
	 */
	@PropertyMapping(skip = SkipPropertyMapping.ON_DEFAULT_VALUE)
	MockMvcPrint print() default MockMvcPrint.DEFAULT;

	/**
	 * If {@link MvcResult} information should be printed only if the test fails.
	 * @return {@code true} if printing only occurs on failure
	 */
	boolean printOnlyOnFailure() default true;

	/**
	 * If a {@link WebClient} should be auto-configured when HtmlUnit is on the classpath.
	 * Defaults to {@code true}.
	 * @return if a {@link WebClient} is auto-configured
	 */
	@PropertyMapping("webclient.enabled")
	boolean webClientEnabled() default true;

	/**
	 * If a {@link WebDriver} should be auto-configured when Selenium is on the classpath.
	 * Defaults to {@code true}.
	 * @return if a {@link WebDriver} is auto-configured
	 */
	@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 {

	/**
	 * The timeout duration for the client (in any format handled by
	 * {@link Duration#parse(CharSequence)}).
	 * @return the web client timeout
	 */
	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");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值