【发布时间】:2017-11-29 22:09:22
【问题描述】:
我试图在 springboot 中进行一些集成测试,因此我使用 @SpringBootTest 注释构建了一些示例测试。我的样本测试是:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration(classes = IntegrationTestConfig.class)
public class WeatherForCityIT {
@Autowired
private TestRestTemplate restTemplate;
@Test
public void getWeatherForExistingCity() throws Exception {
String existingCity = "London";
ResponseEntity<String> responseEntity = restTemplate.getForEntity("/weather/{cityName}",String.class,existingCity.toString());
Assertions.assertThat(responseEntity).isNotNull();
}
}
并具有以下控制器类
@RestController
@RequestMapping("/weather")
public class ChartController {
private WeatherForecastAPI weatherForecastAPI;
@Autowired
public void setWeatherForecastAPI(WeatherForecastAPI weatherForecastAPI) {
this.weatherForecastAPI = weatherForecastAPI;
}
@GetMapping("/{cityName}")
public List<WeatherForecastDTO> get5daysForecast(@PathVariable String cityName) {
weatherForecastAPI.getWeatherForecastByCity(cityWithCountryCode.toString());
}
}
不幸的是,在响应正文中,我收到消息 404 Not Found。在调试模式下,我看到它永远不会到达定义的控制器。从配置的角度来看,我是否遗漏了一些东西?我也尝试使用 MockMvc :
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
@ContextConfiguration(classes = IntegrationTestConfig.class)
public class WeatherForCityIT {
@Autowired
private MockMvc mockMvc;
@Test
public void getWeatherForExistingCity() throws Exception {
String existingCity = "London";
restTemplate.getForEntity("/weather/{cityName}",String.class,existingCity);
mockMvc.perform(get("/weather/" + existingCity))
.andDo(print())
.andExpect(MockMvcResultMatchers.status().isOk());
}
}
但也没有成功(再次是 404 而不是 202)。
已编辑
配置类如下所示:
@Configuration
@EnableAutoConfiguration
public class IntegrationTestConfig {
@Bean
public com.jayway.jsonpath.Configuration configuration() {
return com.jayway.jsonpath.Configuration.builder()
.jsonProvider(new JacksonJsonProvider())
.mappingProvider(new JacksonMappingProvider())
.options(EnumSet.noneOf(Option.class))
.build();
}
}
【问题讨论】:
-
查看您的代码,请求映射看起来不错。您可以做的是检查测试上下文中的可用映射。在您的 WeatherForCityIT 测试类中自动装配 RequestMappingHandlerMapping,并使用
RequestMappingHandlerMapping.getHandlerMethods().keySet();访问映射。检查 /weather/{cityName} 路径是否在此列表中。 -
谢谢伊戈尔,我已经检查过了,我收到了
No qualifying bean of type 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping' available,这让我明白了,我没有在这里粘贴完整的代码。我想念@ConfigurationContext,它指向空配置类。当我删除它时,一切正常。这是为什么 ?为什么指定配置类会导致该错误? -
使用
@ContextConfiguration,您可以指定要加载哪些@Configuration类以在您的测试中配置您的ApplicationContext。通常在测试 Spring Boot 应用程序时不需要它。@SpringBootTest将自动在使用@SpringBootApplication注释的类中搜索您的主要配置。如果您需要调整您的主要配置,最好使用@TestConfiguration类。你可以找到更多here。 -
好的,但是如何处理没有嵌套的配置。这个配置类在很多其他测试中很常见,所以我不想将它复制粘贴到其他类中。因为
@TestConfiguration用于嵌套配置 -
尝试使用
@TestConfiguration而不是@Configuration来注释您的 IntegrationTestConfig。
标签: spring-boot integration-testing resttemplate mockmvc