Mockito -模拟POST请求- ParameterizedTypeReference.forType(String.class

1mrurvl1  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(64)

我正在尝试创建一个单元测试,其中正在发出发布请求。
第一个月
我正在尝试使用Mockit.when(WebClient.post(Mockito.anyString(), null, Mockito.eq(new ParameterizedTypeReference<String> {})).thenReturn(nothing);
我得到了
java.lang.IllegalArgumentException:ParameterizedTypeReference must not be null
如何正确模拟ParameterizedTypeReference.forType(String.class)

qvk1mo1f

qvk1mo1f1#

该错误是由于向ParameterizedTypeReference.forType方法传递空值造成的。

@Test
void test() {
//Initialise uri, request 
ParameterizedTypeReference < String > response = ParameterizedTypeReference.forType(String.class);
String returnValue = “response”;
when(webClient.post().uri(uri).bodyValue(request).retrieve().bodyToMono(any(ParameterizedTypeReference.class)))
.thenReturn(Mono.just(returnValue));
 String result = mockClass.postMethod(uri, request, response);
 assertEquals(returnValue, result);
 }

字符串

相关问题