Я использовал Spring Cloud Gateway MVC в MRE ниже, чтобы потребовать дополнительных @Импорт. Если бы я использовал простую функцию RouterFuction (например, Route().GET("/get", r -> ServerResponse.ok().build()).build()), вся суть была бы это спорный вопрос, поскольку все необходимые bean-компоненты будут неявно предоставлены @WebMvcTest
Это работает:
Код: Выделить всё
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.servlet.function.RouterFunction;
import org.springframework.web.servlet.function.ServerResponse;
import static org.springframework.cloud.gateway.server.mvc.handler.GatewayRouterFunctions.route;
import static org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions.http;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest(controllers = WebMvcTest.ComplimentingConfig.class)
@Import({GatewayServerMvcAutoConfiguration.class,
SslAutoConfiguration.class,
RestClientAutoConfiguration.class})
public class WebMvcTest {
@Autowired
MockMvc mockMvc;
@Test
void test() throws Exception {
mockMvc.perform(get("/get"))
.andExpect(status().isOk());
}
@Configuration
static class ComplimentingConfig {
@Bean
public RouterFunction getRoute() {
return route()
.GET("/get", http("https://httpbin.org/status/200"))
.build();
}
}
}
Код: Выделить всё
@EnableAutoConfiguration
Код: Выделить всё
@WebMvcTest(controllers = WebMvcTest.ComplimentingConfig.class)
@Import(WebMvcTest.ComplimentingConfig.class)
public class WebMvcTest {
// ...
@Configuration
@EnableAutoConfiguration
static class ComplimentingConfig {
Код: Выделить всё
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.cloud.gateway.server.mvc.handler.ProxyExchangeHandlerFunction' available
Java 17, Spring MVC 6.1.5, Весенняя загрузка 3.2.4
Подробнее здесь: https://stackoverflow.com/questions/783 ... vctest-why