平常我们在使用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

相关文章:

  • 2021-07-19
  • 2021-06-09
  • 2021-11-16
  • 2021-08-01
  • 2021-11-07
  • 2021-06-28
  • 2021-09-20
猜你喜欢
  • 2021-08-21
  • 2021-11-09
  • 2022-12-23
  • 2022-12-23
  • 2021-08-11
  • 2022-12-23
  • 2020-10-15
相关资源
相似解决方案