小编Arj*_*agi的帖子

mock resttemplate将服务测试为restFul客户端

我有一个服务类,用春天写的,有一些方法.其中一个充当了如下的宁静消费者:

.....
        HttpEntity request = new HttpEntity<>(getHeadersForRequest());
        RestTemplate restTemplate = new RestTemplate();
        String url = ENDPOINT_URL.concat(ENDPOINT_API1);

        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url)
                .queryParam("param1", parameter1);
        ReportModel infoModel = null;
        try{
            infoModel = restTemplate.exchange(builder.toUriString(), HttpMethod.GET, request, ReportModel.class).getBody();
        }catch (HttpClientErrorException | HttpServerErrorException e){
            e.printStackTrace();
        }
Run Code Online (Sandbox Code Playgroud)

我想用来Mockito模拟我的服务,但每个与restful服务器实例交互的方法都是一个新的RestTemplate.我要创建一个静态类来将它注入我的服务中?

spring unit-testing mockito spring-boot

6
推荐指数
1
解决办法
9884
查看次数

标签 统计

mockito ×1

spring ×1

spring-boot ×1

unit-testing ×1