【发布时间】:2019-03-21 12:54:53
【问题描述】:
给定一个测试类,例如:
@WebMvcTest
@RunWith(SpringRunner.class)
@SpringBootTest(properties = "spring.profiles.active=test")
public class MyControllerTest {
... some tests
}
我得到错误:
java.lang.IllegalStateException:配置错误:发现测试类 [com.example.MyControllerTest] 的多个 @BootstrapWith 声明:[@org.springframework.test.context.BootstrapWith(value=class org.springframework.boot. test.autoconfigure.web.servlet.WebMvcTestContextBootstrapper), @org.springframework.test.context.BootstrapWith(value=class org.springframework.boot.test.context.SpringBootTestContextBootstrapper)]
期望的目标是我只是在运行一个控制器测试,因此出于测试性能的原因不想设置整个上下文 - 我只想要“Web 层”。
我可以删除 @SpringBootTest(properties = "spring.profiles.active=test") 行 - 但是,现在我还没有激活测试配置文件,它可以通过属性以某种方式自定义 Web 上下文,例如将不再应用的杰克逊自定义。有没有办法让“web 层”仅测试并仍然激活弹簧配置文件?
我的环境是javaversion "10.0.2" 2018-07-17,spring boot1.5.16.RELEASE
【问题讨论】:
-
如果您之前不知道,here 可能是几个选项
-
这是一篇很好的文章,展示了各种测试方法,但我没有看到任何与此相关的内容。
-
activate a spring profile是什么意思。您要激活哪个弹簧配置文件? -
自定义的。在这种情况下,它称为
test。即类路径上有一个application-test.properties文件。该文件可以执行诸如设置杰克逊序列化选项之类的操作,这些选项可以更改应用程序的行为。激活配置文件后,结果可能与未激活时不同,因此我想在测试中激活它。
标签: spring-boot spring-test spring-boot-test spring-web spring-test-mvc