作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
考虑以下代码:
public Mono<Void> doStuff() {
return this.requestStuff()
.onStatus(HttpStatus::is5xxServerError,
clientResponse -> {
aMethodIWouldLikeToTest(clientResponse);
return Mono.error(new MyCustomException("First error I would like to test"));
})
.onStatus(HttpStatus::is4xxClientError,
clientResponse -> {
aMethodIWouldLikeToTest(clientResponse);
return Mono.error(new MyCustomException("Second error I would like to test"));
})
.bodyToMono(String.class)
.flatMap(x -> anotherMethodIManagedToTest(x)))
}
我的第一个目标是测试使用以下方法实现的另一个方法IManagedToTest(x):
import org.springframework.web.reactive.function.client.WebClient;
...
@Mock
private WebClient.ResponseSpec responseSpec;
private String desiredInputParam = "There is another black spot on the sun today!";
...
@Test
public void allGood_anotherMethodIManagedToTest_success {
...
ClassUnderTest classUnderTest = new classUnderTest()
ClassUnderTest classUnderTestSpy = spy(classUnderTestSpy);
doReturn(responseSpec).when(classUnderTestSpy).requestStuff();
when(responseSpec.onStatus(any(), any())).thenReturn(responseSpec);
when(responseSpec.bodyToMono(String.class)).thenReturn(Mono.just(desiredInputParam));
Mono<Void> result = classUnderTestSpy.doStuff();
// Bunch of assertions for anotherMethodIManagedToTest(String desiredInputParam) performed with success ...
}
现在我想创建额外的测试来测试 5xxServerError 事件和 4xxClientError 事件,但我很难弄清楚如何:
关于如何执行这些操作有什么建议吗?
请注意,我无法真正使用任何 PowerMock 替代品(如果这是实现我的目标的唯一方法,我仍然感兴趣)所有使用标准 Mockito 的答案都是首选。
最佳答案
对于我迟到的回复,我深表歉意。可以在您的测试用例中使用mockito:
abstract class CustomMinimalForTestResponseSpec implements WebClient.ResponseSpec {
public abstract HttpStatus getStatus();
public WebClient.ResponseSpec onStatus(Predicate<HttpStatus> statusPredicate, Function<ClientResponse, Mono<? extends Throwable>> exceptionFunction) {
if (statusPredicate.test(this.getStatus())) exceptionFunction.apply(ClientResponse.create(HttpStatus.OK).build()).block();
return this;
}
}
when(responseSpecMock.getStatus()).thenReturn(HttpStatus.INTERNAL_SERVER_ERROR);
when(responseSpecMock.onStatus(any(Predicate.class),any(Function.class)))
.thenCallRealMethod();
package com.example.test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.HttpStatus;
import org.springframework.web.reactive.function.client.ClientResponse;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import java.util.function.Function;
import java.util.function.Predicate;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
public class ExperimentalWebClientTest {
@Mock
private WebClient webClientMock;
@InjectMocks
private ExperimentalWebClient experimentalWebClient;
@Mock
private WebClient.RequestHeadersUriSpec requestHeadersUriSpecMock;
@Mock
private WebClient.RequestHeadersSpec requestHeadersSpecMock;
@Mock
private CustomMinimalForTestResponseSpec responseSpecMock;
@Test
void shouldFailsWhenHttpStatus5xx() {
//given
when(webClientMock.get()).thenReturn(requestHeadersUriSpecMock);
when(requestHeadersUriSpecMock.uri(any(Function.class)))
.thenReturn(requestHeadersSpecMock);
when(requestHeadersSpecMock.retrieve()).thenReturn(responseSpecMock);
when(responseSpecMock.getStatus()).thenReturn(HttpStatus.INTERNAL_SERVER_ERROR);
when(responseSpecMock.onStatus(any(Predicate.class), any(Function.class))).thenCallRealMethod();
//when + Then
assertThrows(MyCustomException.class,
() -> experimentalWebClient.doStuff(),
"call fails with Internal Server Error") ;
}
abstract class CustomMinimalForTestResponseSpec implements WebClient.ResponseSpec {
public abstract HttpStatus getStatus();
public WebClient.ResponseSpec onStatus(Predicate<HttpStatus> statusPredicate, Function<ClientResponse, Mono<? extends Throwable>> exceptionFunction) {
if (statusPredicate.test(this.getStatus())) exceptionFunction.apply(ClientResponse.create(HttpStatus.OK).build()).block();
return this;
}
}
}
关于java - 模拟 org.springframework.web.reactive.function.client.WebClient.ResponseSpec#onStatus 输入参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59530512/
我有很多使用 Spring 的 WebClient 中的 onStatus API 的方法: @Override public Mono createAccommodation(CreateAccom
我现在使用 WebClient 而不是其余模板来调用 API。我们的目标是,在不久的将来的冲刺中,我们将使所有客户端都具有响应性和非阻塞性,但在短期内,我们可以使用阻塞调用,但至少要有 WebClie
考虑以下代码: public Mono doStuff() { return this.requestStuff() .onStatus(HttpSta
我是一名优秀的程序员,十分优秀!