案例1:在我的服务类下面调用,其中Employee只是类
ResponseEntity<Employee> response = restTemplate.exchange(url, HttpMethod.GET, entity,Employee.class);
下面用于我的测试类,响应是给出emp对象。
Employee emp = new Employee();
ResponseEntity<Employee> responseEntity = new ResponseEntity<Employee>(emp,HttpStatus.ACCEPTED);
when(restTemplate.exchange(Mockito.anyString(), Mockito.any(), Mockito.any(),Mockito.<Class<Employee>>any())).thenReturn(responseEntity);
案例2:在我的服务类中调用,其中Employee是数组
ResponseEntity<Employee[]> response = restTemplate.exchange(url, HttpMethod.GET, entity,Employee[].class);
我在junit测试中使用下面的内容。但是将响应视为空。
ResponseEntity<Employee[]> responseEntity = new ResponseEntity<Employee[]>(emp,HttpStatus.ACCEPTED);
when(restTemplate.exchange(Mockito.anyString(), Mockito.any(), Mockito.any(),Mockito.<Class<Employee[]>>any())).thenReturn(responseEntity);
如果(1)我正在使用Employee
而在情况(2)中我正在使用Employee[]
。在我的服务和测试中使用Employee[]
时我得到null response(ResponseEntity obj)
但是当我仅在服务和测试中使用Employee
时,我将获得带有emp(stubbed或dummy)的ResponseEntity对象。 / p>
请帮助解释为什么我在第(2)例中得到null。
答案 0 :(得分:-2)
使用此:
ResponseEntity<Employee[]> responseEntity = new ResponseEntity<Employee[]>(emp,HttpStatus.ACCEPTED);
when(restTemplate.exchange(Mockito.anyString(), Mockito.any(), Mockito.any(),Mockito.<Class<Employee[]>>anyObject())).thenReturn(responseEntity);