Java postForEntity的Mockito测试不起作用
我试图用postForEntity调用来模拟这个方法-Java postForEntity的Mockito测试不起作用,java,spring,mockito,Java,Spring,Mockito,我试图用postForEntity调用来模拟这个方法- public AuthorizeClient(RestTemplateBuilder builder, Config config) { this.grantedUrl = config.grantedUrl(); this.restTemplate = HttpClientHelper.getRestTemplate(builder, authorizationConfig); } private final Re
public AuthorizeClient(RestTemplateBuilder builder, Config config) {
this.grantedUrl = config.grantedUrl();
this.restTemplate = HttpClientHelper.getRestTemplate(builder, authorizationConfig);
}
private final RestTemplate restTemplate;
private String grantedUrl;
public List<Permission> getPermissions(
PermissionsRequest permissionsRequest) {
try {
var headers = new HttpHeaders();
var request = new HttpEntity<PermissionsRequest>(permissionsRequest, headers);
var permissions = restTemplate.postForEntity(grantedUrl, request, Permission[].class);
return Arrays.asList(permissions.getBody());
} catch (HttpClientErrorException err) {
logger.error(err);
throw err;
}
}
在这条线上-
var res=authorizeClient.getPermissions(permissionsRequest)
我的客户是这样构造的
请说明我遗漏了什么。坦白地说,没有线索:(
提前感谢尽管对RestTemplate进行了模拟,但由于模拟未到达测试对象,并且方法
authorizeClient::getAllGrantedPermissions
使用其自己的RestTemplate
实现,因此它不会生效
要模拟的RestTemplate
也必须注入到应该使用它的实现中,否则,原始的、真实的实现仍然被使用。使RestTemplate
可注入:
class AuthorizeClient { // You haven't specified the class name
private final RestTemplate restTemplate;
private String grantedUrl;
public AuthorizeClient(RestTemplate restTemplate) { // Dependency injection through constructor
this.restTemplate = restTemplate;
}
public List<Permission> getPermissions(..) { .. }
}
谢谢@Nikolas。这很有帮助。我用更多的信息更新了我的问题。而是针对提出的新问题问一个新问题。这个问题已经得到了回答。请接受答案,或者编辑问题,从我或其他人那里得到更好的答案-不要将问题连成一个问题,并保持其原子性。我相信你会更容易找到答案如果这个问题已经解决,你可以问一个新问题,然后继续前进。谢谢你的建议。我提出了一个新问题。
[ERROR] testAuthorizationPermissions Time elapsed: 0.86 s <<< ERROR!
org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://localhost/v1/permissions": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)
java.lang.IllegalArgumentException: URI is not absolute
class AuthorizeClient { // You haven't specified the class name
private final RestTemplate restTemplate;
private String grantedUrl;
public AuthorizeClient(RestTemplate restTemplate) { // Dependency injection through constructor
this.restTemplate = restTemplate;
}
public List<Permission> getPermissions(..) { .. }
}
@Mock
private RestTemplate restTemplate;
@InjectMock // Injects all necessary @Mock objects
private AuthorizeClient authorizeClient; // An implementation, not an interface
@Test
public void testAuthorizationPermissions() {
Mockito.when(restTemplate.postForEntity(Mockito.anyString(), Mockito.any(), Mockito.any()))
.thenReturn(expGrantedPermissions);
// now it is assured the authorizeClient uses restTemplate and not its own one
var res = authorizeClient.getAllGrantedPermissions(permissionsRequest);
assertNotNull(res);
}