Java 模拟单元测试的请求实体调用
我想模拟请求实体和响应来测试控制器方法上的方法,这段代码是由另一个开发人员编写的,我应该使用mockito来测试它 我试图模拟请求实体值和响应实体值,但它不起作用,并且在尝试调试时出现反射错误Java 模拟单元测试的请求实体调用,java,rest,junit,mockito,resttemplate,Java,Rest,Junit,Mockito,Resttemplate,我想模拟请求实体和响应来测试控制器方法上的方法,这段代码是由另一个开发人员编写的,我应该使用mockito来测试它 我试图模拟请求实体值和响应实体值,但它不起作用,并且在尝试调试时出现反射错误 public class InquiryController { private static final Logger log = LoggerFactory.getLogger(InquiryController.class); @Autowired private Inquir
public class InquiryController {
private static final Logger log =
LoggerFactory.getLogger(InquiryController.class);
@Autowired
private InquiryProperties inquiryProperties;
@Autowired
private InquiryService inquiryService;
@Autowired
RestTemplate restTemplate;
public static int count = 0;
@Bean
private RestTemplate getRestTemplate() {
return new RestTemplate();
}
@PostMapping(value = "/endCustomer", produces = { MediaType.APPLICATION_JSON_VALUE }, consumes = {
MediaType.APPLICATION_JSON_VALUE })
public ResponseEntity<List<EndCustomerDTO>> endCustomer(@RequestBody CustomerInfo customerInfo)
throws IOException, JSONException {
log.info("### InquiryController.endCustomer() ===>");
List<EndCustomerDTO> endCustomerDTOs = null;
try {
//RestTemplate restTemplate = new RestTemplate();
RequestEntity<CustomerInfo> body = RequestEntity.post(new URI(inquiryProperties.getEndCustomer()))
.accept(MediaType.APPLICATION_JSON).body(customerInfo);
ResponseEntity<List<EndCustomerDTO>> response = restTemplate.exchange(body,
new ParameterizedTypeReference<List<EndCustomerDTO>>() {
});
endCustomerDTOs = (response != null ? response.getBody() : new ArrayList<EndCustomerDTO>());
} catch (RestClientException | URISyntaxException e) {
log.error("InquiryController.endCustomer()" + e.getMessage());
}
log.info("### END InquiryController.endCustomer() ===>");
if (null == endCustomerDTOs) {
return new ResponseEntity<List<EndCustomerDTO>>(new ArrayList<EndCustomerDTO>(), HttpStatus.OK);
}
return new ResponseEntity<List<EndCustomerDTO>>(endCustomerDTOs, HttpStatus.OK);
}
公共类查询控制器{
专用静态最终记录器日志=
LoggerFactory.getLogger(InquiryController.class);
@自动连线
私人查询属性查询属性;
@自动连线
私人查询服务查询服务;
@自动连线
rest模板rest模板;
公共静态整数计数=0;
@豆子
私有RestTemplate getRestTemplate(){
返回新的RestTemplate();
}
@PostMapping(value=“/endCustomer”,products={MediaType.APPLICATION\u JSON\u value},consumes={
MediaType.APPLICATION_JSON_VALUE})
公共响应endCustomer(@RequestBody CustomerInfo CustomerInfo)
抛出IOException、JSONException{
log.info(“###InquiryController.endCustomer()==>”;
List ENDCOUSTOMERDTOS=null;
试一试{
//RestTemplate RestTemplate=新RestTemplate();
RequestEntity body=RequestEntity.post(新URI(inquiryProperties.getEndCustomer())
.accept(MediaType.APPLICATION_JSON).body(customerInfo);
ResponseEntity response=restTemplate.exchange(主体,
新的ParameteredTypeReference(){
});
endCustomerDTOs=(response!=null?response.getBody():new ArrayList());
}catch(RestClientException | URISyntaxException e){
log.error(“InquiryController.endCustomer()”+e.getMessage());
}
log.info(“###END InquiryController.endCustomer()==>”;
if(null==endCustomerDTOs){
返回新的ResponseEntity(new ArrayList(),HttpStatus.OK);
}
返回新的响应状态(endCustomerDTOs,HttpStatus.OK);
}
这是因为当您执行REST调用时,RestTemplate
的实例没有通过Spring IOC
注入。您需要在组件类中声明的getRestTemplate
方法,该方法在应用程序启动期间或组件扫描期间被扫描。从而使RestTemplate
可用标签用于autowire
这是因为在执行REST调用时,RestTemplate
的实例没有通过Spring IOC
注入。您需要在组件类中声明的getRestTemplate
方法,该方法在应用程序启动期间或组件扫描期间扫描。因此restemplate
可用于autowire
一旦您按照@chrylis的建议将配置从控制器中分离出来,您就可以继续这样做了
您必须尝试模拟RequestEntity.post方法。请注意,它是一个静态方法,模拟方式与通常的公共实例方法稍有不同。为此,您需要使用PowerMockito,因为Mockito不会这样做
在pom中添加依赖项,如下所示:
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.6.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.6.5</version>
<scope>test</scope>
</dependency>
以及模拟post方法,如下所示:
PowerMockito.mockStatic(RequestEntity.class); when(RequestEntity.post(any(URI.class))).thenReturn(getRequestEntityResponseBody());
private RequestEntity< CustomerInfo > getRequestEntityResponseBody(){
//code
}
PowerMockito.mockStatic(RequestEntity.class);when(RequestEntity.post(any(URI.class))。然后返回(getRequestEntityResponseBody());
私有RequestEntitygetRequestEntityResponseBody(){
//代码
}
更新
CustomerInfo customerInfo = new CustomerInfo();
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("MyResponseHeader", "MyValue");
RequestEntity<CustomerInfo> customerInfoRequestEntity = new ResponseEntity<CustomerInfo>(customerInfo, responseHeaders, HttpStatus.OK);
PowerMockito.mockStatic(RequestEntity.class);
when(RequestEntity.post(any(URI.class))).thenReturn(customerInfoRequestEntity);
CustomerInfo CustomerInfo=new CustomerInfo();
HttpHeaders responseHeaders=新的HttpHeaders();
responseHeaders.set(“MyResponseHeader”、“MyValue”);
RequestEntity CustomerForeQuestentity=新的ResponseEntity(customerInfo,responseHeaders,HttpStatus.OK);
mockStatic(RequestEntity.class);
when(RequestEntity.post(any(URI.class)).thenReturn(CustomerForequenceEntity);
一旦您按照@chrylis的建议将配置从控制器中分离出来,您就可以继续这样做了
您必须尝试模拟RequestEntity.post方法。请注意,它是一个静态方法,模拟方式与通常的公共实例方法稍有不同。为此,您需要使用PowerMockito,因为Mockito不会这样做
在pom中添加依赖项,如下所示:
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.6.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.6.5</version>
<scope>test</scope>
</dependency>
以及模拟post方法,如下所示:
PowerMockito.mockStatic(RequestEntity.class); when(RequestEntity.post(any(URI.class))).thenReturn(getRequestEntityResponseBody());
private RequestEntity< CustomerInfo > getRequestEntityResponseBody(){
//code
}
PowerMockito.mockStatic(RequestEntity.class);when(RequestEntity.post(any(URI.class))。然后返回(getRequestEntityResponseBody());
私有RequestEntitygetRequestEntityResponseBody(){
//代码
}
更新
CustomerInfo customerInfo = new CustomerInfo();
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("MyResponseHeader", "MyValue");
RequestEntity<CustomerInfo> customerInfoRequestEntity = new ResponseEntity<CustomerInfo>(customerInfo, responseHeaders, HttpStatus.OK);
PowerMockito.mockStatic(RequestEntity.class);
when(RequestEntity.post(any(URI.class))).thenReturn(customerInfoRequestEntity);
CustomerInfo CustomerInfo=new CustomerInfo();
HttpHeaders responseHeaders=新的HttpHeaders();
responseHeaders.set(“MyResponseHeader”、“MyValue”);
RequestEntity CustomerForeQuestentity=新的ResponseEntity(customerInfo,responseHeaders,HttpStatus.OK);
mockStatic(RequestEntity.class);
when(RequestEntity.post(any(URI.class)).thenReturn(CustomerForequenceEntity);
看起来您正在…尝试混合配置和控制器?将字段注入替换为构造函数注入,并使用RestOperations
接口而不是RestTemplate
,这样您就可以简单地模拟依赖关系。看起来您正在…尝试混合配置和控制器将字段注入放在构造函数注入中,并使用restcomperations
接口而不是restemplate
,这样您就可以轻松地模拟依赖关系了。您能解释一下为什么返回getRequestEntityResponseBody()吗为什么要编写@raviii11这个方法呢?同一个方法可能有多个测试。在这种情况下,可以重用它来返回RequestEntity,而不是在每个测试中重复创建。如果在本地创建对象就很好了。你能给我举一个在本地创建对象/模拟对象的例子吗ACIN问题,因为它需要url和一个模拟测试@raviiii1@ApurvAdarsh,我已经更新了回复。正如我之前提到的,