我有一个方法GET我需要使用rest模板进行测试:
@ApiOperation("Finds all existing Tag")
@ApiResponses({
@ApiResponse(code = 200, message = "When list of new tag has been found."),
@ApiResponse(code = 404, message = "When unable to find any tag"),
})
@RequestMapping(method = RequestMethod.GET)
@ResponseStatus(value = HttpStatus.OK)
public ResponseEntity<List<Tag>> findAllEvents() {
log.info("Find All Tags");
return ResponseEntity.ok(TagEventRepository.findAllEvents());
}
}
我的测试看起来像这样:
@Test
public void findAllEvents() {
//GIVEN
ParameterizedTypeReference<List<Tag>> newTag = new ParameterizedTypeReference<List<Tag>>(){};
TagTestBuilder
.withFullList()
.withSaved()
.buildList();
//WHEN
ResponseEntity<List<Tag>> response = restTemplate.exchange(TagResourceConstants.PATH, HttpMethod.GET, null, newTag);
//THEN
assertEquals(HttpStatus.OK.value(),response.
getStatusCodeValue());
}
我的问题是我无法将参数化类型引用(继续将List作为返回实体)链接到tagTestBuilder,后者创建完整的标记列表并扩展TestBuilder抽象类,该类实现诸如.createFullList,save和builtList
使用上面的版本测试将通过 - 但它不正确,因为它没有构建我需要的对象列表(不使用TestBuilder)。
我当然可以用testBuilder链接列表但是我无法解析restTemplate交换的方法,例如:
List<Tag> newTag = (List<Tag>) TagTestBuilder
.withFullList()
.withSaved()
.buildList();
//WHEN
ResponseEntity<List<Tag>> response = restTemplate.exchange(TagResourceConstants.PATH, HttpMethod.GET, null, newTag);
//THEN
assertEquals(HttpStatus.OK.value(),response.
getStatusCodeValue());
}
我得到以下错误,了解表单不正确,但不知道如何正确编写。
Error:(82, 71) java: no suitable method found for exchange(java.lang.String,org.springframework.http.HttpMethod,<nulltype>,java.util.List<Tag>)
method org.springframework.boot.test.web.client.TestRestTemplate.<T>exchange(java.lang.String,org.springframework.http.HttpMethod,org.springframework.http.HttpEntity<?>,java.lang.Class<T>,java.lang.Object...) is not applicable
(cannot infer type-variable(s) T
(argument mismatch; java.util.List<Tag> cannot be converted to java.lang.Class<T>))
method org.springframework.boot.test.web.client.TestRestTemplate.<T>exchange(java.lang.String,org.springframework.http.HttpMethod,org.springframework.http.HttpEntity<?>,java.lang.Class<T>,java.util.Map<java.lang.String,?>) is not applicable
(cannot infer type-variable(s) T
(actual and formal argument lists differ in length))
method org.springframework.boot.test.web.client.TestRestTemplate.<T>exchange(java.net.URI,org.springframework.http.HttpMethod,org.springframework.http.HttpEntity<?>,java.lang.Class<T>) is not applicable
(cannot infer type-variable(s) T
(argument mismatch; java.lang.String cannot be converted to java.net.URI))
method org.springframework.boot.test.web.client.TestRestTemplate.<T>exchange(java.lang.String,org.springframework.http.HttpMethod,org.springframework.http.HttpEntity<?>,org.springframework.core.ParameterizedTypeReference<T>,java.lang.Object...) is not applicable
(cannot infer type-variable(s) T
(argument mismatch; java.util.List<Tag> cannot be converted to org.springframework.core.ParameterizedTypeReference<T>))
method org.springframework.boot.test.web.client.TestRestTemplate.<T>exchange(java.lang.String,org.springframework.http.HttpMethod,org.springframework.http.HttpEntity<?>,org.springframework.core.ParameterizedTypeReference<T>,java.util.Map<java.lang.String,?>) is not applicable
(cannot infer type-variable(s) T
(actual and formal argument lists differ in length))
method org.springframework.boot.test.web.client.TestRestTemplate.<T>exchange(java.net.URI,org.springframework.http.HttpMethod,org.springframework.http.HttpEntity<?>,org.springframework.core.ParameterizedTypeReference<T>) is not applicable
(cannot infer type-variable(s) T
(argument mismatch; java.lang.String cannot be converted to java.net.URI))
method org.springframework.boot.test.web.client.TestRestTemplate.<T>exchange(org.springframework.http.RequestEntity<?>,java.lang.Class<T>) is not applicable
(cannot infer type-variable(s) T
(actual and formal argument lists differ in length))
method org.springframework.boot.test.web.client.TestRestTemplate.<T>exchange(org.springframework.http.RequestEntity<?>,org.springframework.core.ParameterizedTypeReference<T>) is not applicable
(cannot infer type-variable(s) T
(actual and formal argument lists differ in length))
还尝试使用getEntity方法编写此测试,最后这似乎有效,但我必须调试测试以确保:
@Test
public void findAllEvents() {
//GIVEN
List<Tag> newTag = (List<Tag>) TagTestBuilder
.withFullList()
.withSaved()
.buildList();
//WHEN
ResponseEntity<Tag[]> response = restTemplate.getForEntity(TagResourceConstants.PATH, Tag[].class, newTag);
//THEN
assertEquals(HttpStatus.OK.value(),response.
getStatusCodeValue());
我认为这个测试的两个版本(带有[]的getEntity并使用参数化类型引用,它们似乎都正常工作。
答案 0 :(得分:0)
restTemplate.exchage
中的第四个参数是responseType
,它只告诉exchange
方法应该转换响应的类。它接受Class
类型的变量。如果响应是对象列表,它也可以接受ParameterizedTypeReference
。
向该参数提供数据(即您的案例中的标记列表)没有意义。
你的第一个代码块是对的。您不需要将Tags
列表传递给restTemplate
方法
ParameterizedTypeReference<List<Tag>> responseType = new ParameterizedTypeReference<List<Tag>>(){};
ResponseEntity<List<Tag>> response = restTemplate.exchange(TagResourceConstants.PATH, HttpMethod.GET, null, responseType);