Spring Boot 集成测试 - TestRestTemplate 未到达控制器并获得 404 而不是 200

SpringBoot integration test - TestRestTemplate doesn't reach controller and get 404 instead of 200

我试图在 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();


            }
        }

并有以下控制器class

@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)。

已编辑

配置 class 如下所示:

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

}

您的测试配置 class 中不需要 @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...
}

关于您收到的异常消息:

No qualifying bean of type 'com.jayway.jsonpath.Configuration' available: expected single matching bean but found 2: getConfiguration,configuration)

根据错误消息,您知道上下文中有 2 个相同类型 (com.jayway.jsonpath.Configuration) 的 bean

  1. 名称为 getConfiguration
  2. 的 Bean
  3. 名称为配置的Bean

bean configuration 在您的 IntegrationTestConfig 中定义,另一个 bean getConfiguration 已定义在您的配置之一 classes 中。在您的应用程序的某个地方,您正在按类型自动装配 'com.jayway.jsonpath.Configuration' bean。由于您有 2 个这种类型的 bean,Spring 正在抱怨异常。

你需要这两种豆子吗?如果不是,请删除其中一个 bean。否则考虑在自动装配 bean 时使用 @Qualifier 注释。