如何模拟 RestTemplate Exchange spring boot

non*_*jai 2 automated-tests mockito spring-boot junit5

我尝试了很多方法来模拟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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我的模拟如下:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

我还尝试返回评论的响应实体。但总是得到例外的交换。

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

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

感谢您的支持。

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

non*_*jai 5

当我将方法更新为非静态并将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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

以及带有 verify 的模拟:

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())
        )
    }
}
Run Code Online (Sandbox Code Playgroud)