您的位置:首页 > 运维架构 > Tomcat

Spring-boot非Mock测试MVC,调试启动tomcat容器

2016-12-11 15:41 513 查看
平常我们在使用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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: