WebMvcTest与@SpringBootTest结合的问题

问题描述 投票:0回答:1

我正在使用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());
        }

        }

我得到了下面的Exception,谁能帮助我了解我缺少什么?

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个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.