首页 > 技术文章 > Spring-boot非Mock测试MVC,调试启动tomcat容器

lanhj 2016-12-11 15:41 原文

平常我们在使用spring-boot去debug一个web应用时,通常会使用MockMvc。

如下配置:

@RunWith(value = SpringRunner.class)
@SpringBootTest(classes = xxxx.class)

但是这样并不会真的打开嵌入式servlet容器

spring不愧是个强大的框架,心想着它肯定有配置真正打开容器的地方,然后带着想法去看源码,果然发现了可配置的东西~

这样配置就好了:

@RunWith(value = SpringRunner.class)
@SpringBootTest(
    webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,
    classes = xxxx.class
)

 

在默认情况下,SpringRunner给SpringApplication这个类设置了applicationContextClass(GenericWebApplicatoinContext),这个时候SpringApplication将不会去初始化applicationContextClass为AnnotationConfigEmbeddedWebApplicationContext

推荐阅读