【发布时间】:2022-06-12 04:05:58
【问题描述】:
我正在测试一个使用 Spring Boot 的 webflux 库的类,我遇到了 StepVerifier::expectError 的奇怪行为。具体来说,我可以将 any 类型(甚至是 String!)传递给方法并且测试通过。对于这个特定的测试,我的被测方法应该以错误 Mono 响应,并且该 mono 应该包含一个自定义异常。我对this SO question 的理解是我的StepVerifier 在正确的块中运行。这里出了什么问题?
被测类:
@Service
@RequiredArgsConstructor
public class PaymentsBO {
private final ContractClient contractClient;
public Mono<Void> updatePaymentInfo(Request record) {
return contractClient
.getContract(UUID.fromString(record.getContractUuid()))
.onErrorResume(throwable -> Mono.error(() -> new CustomException(
"Contract Service responded with a non-200 due to "
+ throwable.getCause())))
.flatMap(
// happy-path logic
);
}
}
单元测试:
def "Returns an error if the Contract Service returns a non-200"() {
given:
def testSubject = new PaymentsBO(contractServiceMock)
def contractServiceMock = Mock(ContractClient)
when:
def result = testSubject.updatePaymentInfo(record)
and:
StepVerifier.create(result)
.expectError(String.class)
then:
1 * contractServiceMock.getContract(CONTRACT_UUID) >> Mono.error(new ContractServiceException())
}
【问题讨论】:
标签: groovy spring-webflux project-reactor spock reactive