我试图在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)。
EDITED
配置类如下所示:
@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();
}
}
答案 0 :(得分:1)
您的测试配置类中不需要@EnableAutoConfiguration
。因此, IntegrationTestConfig 应如下所示:
@TestConfiguration
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 应保留为示例代码:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration(classes = IntegrationTestConfig.class)
public class WeatherForCityIT {
// your code here...
}
关于您收到的异常消息:
没有符合条件的bean&#39; com.jayway.jsonpath.Configuration&#39; available:期望的单个匹配bean但找到2:getConfiguration,configuration)
从错误消息中您知道您的上下文中有相同类型的 2个bean (com.jayway.jsonpath.Configuration
):
bean 配置在 IntegrationTestConfig 中定义,另一个bean getConfiguration 在您的一个配置类中定义。在你的应用程序的某个地方你正在自动装配&com; jayway.jsonpath.Configuration&#39;豆类型。由于你有2个这种类型的bean,Spring抱怨异常。
你需要这两种豆吗?如果没有,删除其中一个bean。否则,请考虑在自动装配bean时使用@Qualifier注释。