如何模拟 RestTemplate Exchange spring 启动
How to mock a RestTemplate Exchange spring boot
我已经尝试了很多方法来模拟 restTemplate 交换,但模拟没有发生,实际交换一直在调用并给我 url 无效异常。
我的 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);
}
}
}
下面是我的模拟:
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);
}
}
我也尝试 return 评论响应实体。但总是有例外作为交换。
我对 mocking 的理解是不会调用实际的交换方法,那么我如何获得 resttemplate 交换异常。
如果需要任何输入,请发表评论。
感谢支持
UPDATE:
I tried with changing the static method to non static but kept the test case as is. But i get the same error
当我将方法更新为非静态并将 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())
)
}
}
我已经尝试了很多方法来模拟 restTemplate 交换,但模拟没有发生,实际交换一直在调用并给我 url 无效异常。
我的 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);
}
}
}
下面是我的模拟:
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);
}
}
我也尝试 return 评论响应实体。但总是有例外作为交换。
我对 mocking 的理解是不会调用实际的交换方法,那么我如何获得 resttemplate 交换异常。
如果需要任何输入,请发表评论。
感谢支持
UPDATE: I tried with changing the static method to non static but kept the test case as is. But i get the same error
当我将方法更新为非静态并将 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())
)
}
}