Java 当出现';这是一个请求机构
关于模拟WebClient对象,有几个问题和有用的答案。但是当我在一个机构做文章时,我仍然有问题。我只是在使用Mockito而不是mockwebserver 这是我正在测试的方法:Java 当出现';这是一个请求机构,java,spring,unit-testing,mockito,webclient,Java,Spring,Unit Testing,Mockito,Webclient,关于模拟WebClient对象,有几个问题和有用的答案。但是当我在一个机构做文章时,我仍然有问题。我只是在使用Mockito而不是mockwebserver 这是我正在测试的方法: public class RestClient extends BaseRestClient { ... public <T,G> Mono<T> post(String url, G req, Class<T> resp) throws IOException {
public class RestClient extends BaseRestClient {
...
public <T,G> Mono<T> post(String url, G req, Class<T> resp) throws IOException {
Mono<T> response = null;
response = this.getWebClient().post()
.uri(url)
.header(HttpHeaders.CONTENT_TYPE,JSON_CONTENT_TYPE)
.accept(MediaType.APPLICATION_JSON)
//.body(BodyInserters.fromObject(req))
.header(HttpHeaders.AUTHORIZATION, BEARER + token)
.retrieve()
.bodyToMono(resp).log();
return response.map(resp::cast);
}
...
但仍然没有成功。有什么问题吗?如果使用
@InjectMocks
,则无需创建新的RestClient()
。
另外,由于您正在模拟WebClient
,因此不需要模拟WebClient.
*`
因此,代码变为
@Mock
WebClient webClient;
@InjectMocks
RestClient restClient;
@Test
public void postTest() throws IOException {
when(webClient.post()).thenReturn(requestBodyUriSpec);
when(requestBodyUriSpec.uri(anyString())).thenReturn(requestBodySpec);
when(requestBodySpec.header(any(),any())).thenReturn(requestBodySpec);
when(requestBodySpec.accept(any())).thenReturn(requestBodySpec);
when(responseSpec.bodyToMono(ArgumentMatchers.<Class<String>>notNull()))
.thenReturn(Mono.just("resp"));
//when(requestBodySpec.body(any())).thenReturn(requestHeadersSpec);
when(requestBodySpec.retrieve()).thenReturn(responseSpec);
restClient.post("http://sampleurl",Object.class, Object.class);
}
@Mock
网络客户端网络客户端;
@注射模拟
RestClient-RestClient;
@试验
public void postest()引发IOException{
when(webClient.post())。然后返回(requestBodyUriSpec);
when(requestBodyUriSpec.uri(anyString())。然后return(requestBodySpec);
when(requestBodySpec.header(any(),any())。然后return(requestBodySpec);
当(requestBodySpec.accept(any())。然后返回(requestBodySpec);
when(responseSpec.bodyToMono(ArgumentMatchers.notNull()))
.然后返回(单声道正义(“resp”);
//when(requestBodySpec.body(any())。然后返回(requestHeadersSpec);
当(requestBodySpec.retrieve())。然后返回(responseSpec);
restClient.post(“http://sampleurl“,Object.class,Object.class);
}
我错过了模拟requestHeadersSpec的“header”方法:
when(requestHeadersSpec.header(any(),any())).thenReturn(requestHeadersSpec);
所以,现在这很好:
@Test
public void postTest() throws IOException {
when(webClient.post()).thenReturn(requestBodyUriSpec);
when(requestBodyUriSpec.uri(anyString())).thenReturn(requestBodySpec);
when(requestBodySpec.header(any(),any())).thenReturn(requestBodySpec);
when(requestHeadersSpec.header(any(),any())).thenReturn(requestHeadersSpec);
when(requestBodySpec.accept(any())).thenReturn(requestBodySpec);
when(requestBodySpec.body(any())).thenReturn(requestHeadersSpec);
when(requestHeadersSpec.retrieve()).thenReturn(responseSpec);
when(responseSpec.bodyToMono(ArgumentMatchers.<Class<String>>notNull()))
.thenReturn(Mono.just("resp"));
Assert.assertNotNull(restClient.post("http://sampleurl",Object.class, Object.class));
}
@测试
public void postest()引发IOException{
when(webClient.post())。然后返回(requestBodyUriSpec);
when(requestBodyUriSpec.uri(anyString())。然后return(requestBodySpec);
when(requestBodySpec.header(any(),any())。然后return(requestBodySpec);
when(requestHeadersSpec.header(any(),any())。然后return(requestHeadersSpec);
当(requestBodySpec.accept(any())。然后返回(requestBodySpec);
when(requestBodySpec.body(any())。然后返回(requestHeadersSpec);
当(requestHeadersSpec.retrieve())。然后返回(responseSpec);
when(responseSpec.bodyToMono(ArgumentMatchers.notNull()))
.然后返回(单声道正义(“resp”);
Assert.assertNotNull(restClient.post(“http://sampleurl“,Object.class,Object.class”);
}
在我的WebClient代码中添加标题后,它开始像魔术一样工作
@Test
public void postMethod() {
when(webClient.post()).thenReturn(requestBodyUriMock);
when(requestBodyUriMock.uri(anyString())).thenReturn(requestBodyMock);
when(requestBodyMock.header(any(),any())).thenReturn(requestBodyMock);
when(requestHeadersMock.header(any(),any())).thenReturn(requestHeadersMock);
when(requestBodyMock.accept(any())).thenReturn(requestBodyMock);
when(requestBodyMock.contentType(any())).thenReturn(requestBodyMock);
when(requestBodyMock.body(any())).thenReturn(requestHeadersMock);
when(requestHeadersMock.retrieve()).thenReturn(responseMock);
when(responseSpec.bodyToMono(String.class))
.thenReturn(Mono.just("output"));
//WebClient call
TestResponse test = service.testMethod(mockObject);
assertEquals(test.Status(), 200);
}
是的,我错误地实例化了restClient。谢谢你的指点!不过,别以为这就是它不起作用的原因。应该解决这个问题,不要让更多的人感到困惑:)但是,如果您没有对WebClient.*对象进行模拟,那么这些对象在代码片段中的位置在哪里?因为您已经模拟了WebClient,所以可以对WebClient中的所有方法进行模拟,而无需显式模拟它们。这个建议在你的案例中也有用,但是你的代码片段中仍然有它们。比如“when(webClient.post()).thenReturn(requestBodyUriSpec);”其中包含“requestBodyUriSpec”。这个物体从哪里来?酷!是的,加上头球也帮了我。这就是为什么我将上面的答案标记为已接受。@Alireza是的,谢谢!!!responseMock mock来自哪里?您可以像这样将其添加到测试类中@Mock private WebClient.RequestBodyUriSpec requestBodyUriMock;服务器将其配置为。。。当(webClientMock.post())。然后返回(requestBodyUriMock);when(requestBodyUriMock.uri('/uri'))。然后返回(requestBodyMock);完全模仿
webclient
是很可怕的,但这是唯一的方法
@Test
public void postTest() throws IOException {
when(webClient.post()).thenReturn(requestBodyUriSpec);
when(requestBodyUriSpec.uri(anyString())).thenReturn(requestBodySpec);
when(requestBodySpec.header(any(),any())).thenReturn(requestBodySpec);
when(requestHeadersSpec.header(any(),any())).thenReturn(requestHeadersSpec);
when(requestBodySpec.accept(any())).thenReturn(requestBodySpec);
when(requestBodySpec.body(any())).thenReturn(requestHeadersSpec);
when(requestHeadersSpec.retrieve()).thenReturn(responseSpec);
when(responseSpec.bodyToMono(ArgumentMatchers.<Class<String>>notNull()))
.thenReturn(Mono.just("resp"));
Assert.assertNotNull(restClient.post("http://sampleurl",Object.class, Object.class));
}
@Test
public void postMethod() {
when(webClient.post()).thenReturn(requestBodyUriMock);
when(requestBodyUriMock.uri(anyString())).thenReturn(requestBodyMock);
when(requestBodyMock.header(any(),any())).thenReturn(requestBodyMock);
when(requestHeadersMock.header(any(),any())).thenReturn(requestHeadersMock);
when(requestBodyMock.accept(any())).thenReturn(requestBodyMock);
when(requestBodyMock.contentType(any())).thenReturn(requestBodyMock);
when(requestBodyMock.body(any())).thenReturn(requestHeadersMock);
when(requestHeadersMock.retrieve()).thenReturn(responseMock);
when(responseSpec.bodyToMono(String.class))
.thenReturn(Mono.just("output"));
//WebClient call
TestResponse test = service.testMethod(mockObject);
assertEquals(test.Status(), 200);
}