Search code examples
spring-boot-testmockmvc

Issue with WebMvcTest in combination with @SpringBootTest


I'm using spring-boot 2.1.7-RELEASE and writing a test to test my rest controller.

Here is my test code

        @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());
        }

        }

I'm getting below Exception, can someone please help me understand what am i missing here?

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

Solution

  • I got the reason behind the issue. Its happening because 'WebMvcTest' using '@BootstrapWith(WebMvcTestContextBootstrapper.class)' and the SpringBootTest referring '@BootstrapWith(SpringBootTestContextBootstrapper.class)'and hence the error.