当前位置: 首页 > 知识库问答 >
问题:

SpringBoot集成测试-TestRestTemplate没有到达controller,得到的是404而不是200

笪建章
2023-03-14

我试图在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未找到。在调试模式下,我看到它永远不会到达定义的控制器。从配置的角度来看,我是否错过了什么?我还试图使用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();
    }

}

共有1个答案

狄雅珺
2023-03-14

测试配置类中不需要启用自动配置(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...
}

关于您收到的异常消息:

没有“com”类型的合格bean。乱穿马路。jsonpath。配置“可用:需要单个匹配bean,但找到2:getConfiguration,Configuration)

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

  1. 名为getConfiguration的Bean

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

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

 类似资料: