Is @WebMvcTest @SpringBootApplication annotation required?

My goal is to port the Spring boot application previously developed with Spring Boot 1.3 to the newest version of Spring to download version 1.4. An application consists of several maven modules, and only one of them contains a class annotated with @SpringBootApplication.

One part of the migration is to use annotations @WebMvcTestto effectively test controllers, and this is where the problem arises.

Consider the application from the Spring github download page. @WebMvcTestthe annotation works fine because, as far as I understand (after several tests), the main package is annotated @SpringBootApplication. Please note that I adhere to the same concept as in the example above for my own tests @WebMvcTest.

The only difference that I see in my application is that the controller classes are located in a separate maven module (without @SpringBootApplicationan annotated class), but with @Configuration and configurations SpringBootConfiguration. If I do not comment on any class with @SpringBootApplication, I always get approval while testing the controller. My contention is the same as when SampleTestApplication class in the above example is modified to have only @EnableAutoConfigurationand @SpringBootConfigurationannotations ( @SpringBootApplicationno):

getVehicleWhenRequestingTextShouldReturnMakeAndModel(sample.test.web.UserVehicleControllerTests)  Time elapsed: 0.013 sec  <<< FAILURE!
java.lang.AssertionError: Status expected:<200> but was:<404>
    at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:54)
    at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:81)
    at org.springframework.test.web.servlet.result.StatusResultMatchers$10.match(StatusResultMatchers.java:664)
    at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:171)
   at sample.test.web.UserVehicleControllerTests.getVehicleWhenRequestingTextShouldReturnMakeAndModel(UserVehicleControllerTests.java:68)

? , @SpringBootApplication, @WebMvcTest?

EDIT 1: maven . . NoSuchBeanDefinitionException , . "" @SpringBootApplication - .

2: EDIT 1, . @ComponentScan , , beans . , @Controller bean ( @WebMvcTest (... class)) @WebMvcTest.

EDIT 3: Spring issue.

+4
2

: .

:

, @WebMvcTest SpringBootApplication, WebMvcTest - (SpringBootApplication ).

, - , , SampleTestConfiguration, @ScanPackages - beans.

src/main/java/sample/test

:
@SpringBootApplication
public class SampleTestConfiguration {

}

:

@RunWith(SpringRunner.class)
@WebMvcTest(MyController.class)
public class MyControllerTest {

    @Autowired
    private MockMvc mvc;

    @MockBean
    private MyService ms;

    @Autowired
    private ApplicationContext context;

    @Test
    public void getDataAndExpectOkStatus() throws Exception {
        given(ms.execute("1")).willReturn(false);
        mvc.perform(get("/1/data").accept(MediaType.APPLICATION_JSON_VALUE)).andExpect(status().isOk()).andExpect(content().string("false"));
    }

    @Test
    public void testMyControllerInAppCtx() {
        assertThat(context.getBean(MyController.class), is(not(nullValue())));
    }

    @Test
    public void testNoMyAnotherControllerInAppCtx() {
        try {
            context.getBean(MyAnotherController.class);
            fail("Bean exists");
        } catch (BeansException e) {
            // ok
        }
    }
}

@WebMvcTest SpringBootApplication, beans (. ):

@WebMvcTest MVC Spring beans @Controller, @ControllerAdvice, @JsonComponent, , WebMvcConfigurer HandlerMethodArgumentResolver. @Component beans .

WebMvcTest SpringBootApplication: WebMvcTest AutoConfiguration, SpringBoot. AutoConfiguration, .

WebMvcTest - SpringBootApplication, , beans . SpringBootApplication, WebMvcTest?

+1

, spring

, , @SpringBootApplication @SpringBootConfiguration. , .

, @WebMvcTest, spring boot beans, , TypeExcludeFilter .

`` `java

@RunWith(SpringRunner.class)
@WebMvcTest(controllers = {JzYsController.class} )
public class JzYsControllerTest {

    private static final String REST_V4_JZYS = "/rest/v4/JzYs";

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private JzYsService service;

    @Test
    public void deleteYsByMlbh() throws Exception {
        Mockito.when(service.deleteYsByMlbh(Mockito.anyString())).thenReturn(Optional.of(1));
        mockMvc.perform(delete(REST_V4_JZYS + "?mbbh=861FA4B0E40F5C7FECAF09C150BF3B01"))
        .andExpect(status().isNoContent());
    }

    @SpringBootConfiguration
    @ComponentScan(excludeFilters = @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class))
    public static class config{
    }
}

`` `

0

Source: https://habr.com/ru/post/1650885/


All Articles