【问题标题】:How to mock a RestTemplate Exchange spring boot如何模拟 RestTemplate Exchange 弹簧靴
【发布时间】:2021-11-11 04:16:51
【问题描述】:

我尝试了很多方法来模拟 restTemplate 交换,但模拟没有发生,实际的交换继续调用并给我 url not valid 异常。

下面是我的 CallRestService 方法

public class Util{
    
    public static ResponseEntity<String> callRestService(JSONObject reqJsonObj,HttpHeaders headers, String url, HttpMethod method, boolean isAuto){
        ResponseEntity<String> re=null;
        try{
            HttpEntity<String> entity=null;
            entity=new HttpEntity<>(String.valueOf(reqJsonObj),headers);
            RestTemplate restTemplate= new RestTemplate();
            re=restTemplate.exchange(url,method,entity,String.class);
        }catch(Exception e){
            System.out.println(e);
        }
    }
}

我的 Mock 如下:

public class UtilTest{
    @InjectMocks
    Util util;
    @Mock
    RestTemplate restTemplate;
    ResponseEntity res=mock(ResponseEntity.class);

    @Test
    public void test(){
        //ResponseEntity<String> entity=new ResponseEntity<String>("anySt",HttpStatus.ACCEPTED);
        Mockito.when(restTemplate.exchange(
                ArgumentMatchers.anyString(),
                ArgumentMatchers.any(HttpMethod.class),
                ArgumentMatchers.any(HttpEntity.class),
                ArgumentMatchers.<Class<String>>any())
        ).thenReturn(res);
        Util.callRestService(json,headers,url,HttpMethod.POST,false);
    }
}

我还尝试返回注释响应实体。但总是得到例外作为交换。

我对模拟的理解是不会调用实际的交换方法,那么我是如何得到resttemplate交换异常的。

如果需要任何输入,请评论。

感谢您的支持。

更新: 我尝试将静态方法更改为非静态,但保持测试用例不变。但我得到同样的错误

【问题讨论】:

  • 异常说:调用 REST 服务时发生异常 - uri 不是绝对的。
  • 为什么一切都是静态的?尝试通过构造函数注入模拟
  • 我无法更改静态方法,但我在研究期间将 RestTemplate 声明保留在外面。静态方法由项目组完成
  • 您不能注入模拟,因为其余模板在函数中初始化。如果是由项目组完成,他们负责为此代码编写测试。
  • 感谢@fuat,我将 resttemplate 声明放在函数之外。并在后台运行测试实际上通过了。

标签: spring-boot automated-tests mockito junit5


【解决方案1】:

当我将方法更新为非静态并将 Resttemplate 声明移到方法外时,问题得到解决。

更新的代码如下,如果有人觉得有用的话。

方法变化:

public class Util{
    private RestTemplate restTemplate= new RestTemplate();
    public ResponseEntity<String> callRestService(JSONObject reqJsonObj,HttpHeaders headers, String url, HttpMethod method, boolean isAuto){
        ResponseEntity<String> re=null;
        try{
            HttpEntity<String> entity=null;
            entity=new HttpEntity<>(String.valueOf(reqJsonObj),headers);
            re=restTemplate.exchange(url,method,entity,String.class);
        }catch(Exception e){
            System.out.println(e);
        }
    }
}

还有带有验证的模拟:

public class UtilTest{
    @InjectMocks
    Util util;
    @Mock
    RestTemplate restTemplate;

    @Test
    public void test(){
        ResponseEntity<String> entity=new ResponseEntity<String>("anySt",HttpStatus.ACCEPTED);
        Mockito.when(restTemplate.exchange(
                ArgumentMatchers.anyString(),
                ArgumentMatchers.any(HttpMethod.class),
                ArgumentMatchers.any(HttpEntity.class),
                ArgumentMatchers.<Class<String>>any())
        ).thenReturn(entity);

        Util.callRestService(json,headers,url,HttpMethod.POST,false);

        Mockito.verify(restTemplate,times(1)).exchange(
                ArgumentMatchers.anyString(),
                ArgumentMatchers.any(HttpMethod.class),
                ArgumentMatchers.any(HttpEntity.class),
                ArgumentMatchers.<Class<String>>any())
        )
    }
}

【讨论】:

    猜你喜欢
    • 2018-11-22
    • 2017-12-22
    • 2018-02-14
    • 2016-08-30
    • 2019-05-03
    • 2021-04-20
    • 1970-01-01
    • 2023-01-07
    • 1970-01-01
    相关资源
    最近更新 更多