【问题标题】:Issue with WebMvcTest in combination with @SpringBootTestWebMvcTest 与 @SpringBootTest 结合的问题
【发布时间】:2020-08-16 01:16:53
【问题描述】:

我正在使用 spring-boot 2.1.7-RELEASE 并编写一个测试来测试我的休息控制器。

这是我的测试代码

        @RunWith(SpringRunner.class)
            @WebMvcTest(MyRESTController.class)
@SpringBootTest(
        webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,classes = {TestConfig.class}
        )
        public class TestMyRESTController {

        @Autowired
        private MockMvc mvc;

        @Before
        public void setUp() {
            MockitoAnnotations.initMocks(this);
        }

        @Test
        public void getAccount()throws Exception {
         mockMvc.perform(get("/user/1"))
                .andDo(print())
                .andExpect(status().isOk());
        }

        }

我遇到了异常,有人可以帮我理解我在这里缺少什么吗?

java.lang.IllegalStateException: Configuration error: found multiple declarations of @BootstrapWith for test class [com.philips.sapphire.infrastructure.SapphireGatewayV1.service.SampleTest]: [@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)]

【问题讨论】:

    标签: spring-boot-test mockmvc


    【解决方案1】:

    我找到了问题背后的原因。它的发生是因为 'WebMvcTest' 使用 '@BootstrapWith(WebMvcTestContextBootstrapper.class)' 和 SpringBootTest 引用 '@BootstrapWith(SpringBootTestContextBootstrapper.class)' 并因此出现错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-13
      • 1970-01-01
      • 1970-01-01
      • 2017-02-13
      相关资源
      最近更新 更多