【发布时间】:2015-04-29 09:55:11
【问题描述】:
我有一种情况,我们想在启动时检查 Spring MVC 代码并在不满足某些条件时抛出异常(从而导致 ApplicationContext 失败)。
有没有办法检测 JUnit 测试来捕获 Spring 启动(特别是在大多数情况下是 Spring Boot)异常,以便它们不会导致测试失败?如果异常没有发生,我基本上希望测试失败。
【问题讨论】:
-
将在测试方法中使用您的 beans xml 描述以编程方式初始化 Spring,并使用
@Test(expected=SomeException.class)注释该方法,是吗? -
或者使用@Rule ExpectedException 甚至更好:blog.codeleak.pl/2014/03/…
-
@Victor Sorokin,我不确定这是否可行。我目前正在注释我的单元测试:
@RunWIth(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = MyApplicationObjects.class) // this one has the controller i want to test @WebAppConfiguration恐怕这会为所有测试方法初始化一次 Spring 应用程序。我一定会去看看的。
标签: java spring spring-mvc junit spring-boot