是否可以在WebMvcTest中激活弹簧配置文件

时间:2018-10-16 22:22:58

标签: spring-boot spring-test spring-boot-test spring-web spring-test-mvc

给出如下测试类:

@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)]

期望的目标是我只是在运行控制器测试,因此出于测试性能的原因,不想设置整个上下文-我只需要“网络层”。

我可以删除@SpringBootTest(properties = "spring.profiles.active=test")行-但是,现在我还没有激活测试配置文件,该配置文件可以通过属性(例如,不再应用的杰克逊自定义设置。有没有一种方法可以只对“ Web层”进行测试并仍然激活弹簧轮廓?

我的环境是Java version "10.0.2" 2018-07-17,春季启动1.5.16.RELEASE

1 个答案:

答案 0 :(得分:4)

要设置有效的个人资料,您可以像这样使用@ActiveProfiles

@WebMvcTest
@RunWith(SpringRunner.class)
@ActiveProfiles("test")
public class MyControllerTest  {

然后您可以application-test yml或测试资源中的属性。