简体   繁体   English

SPOCK:如何模拟供应商行为

[英]SPOCK: How to mock supplier behavior

I am trying to cover the positive negative scenario while executing a supplier inside CompletableFuture . 我试图在CompletableFuture内部执行supplier时掩盖积极的消极情况。 For some reason the mocked values are not getting passed within the supplier . 由于某些原因,模拟值未在supplier内部传递。 My unit test cases are written using spock framework, and since I am not that familiar with this framework, I am not sure that I am mistaking while mocking or there is something with the supplier mocking that I am missing. 我的单元测试用例是使用spock框架编写的,由于我不太熟悉该框架,因此我不确定在进行模拟时我是否会犯错,或者供应商在进行模拟时会遗漏某些东西。

Code under test: 被测代码:

CompletableFuture
    .supplyAsync(() -> s3Service.upload(bucket, key, file), executor)
    .handle(((putObjectResult, throwable) -> {
        if (throwable != null) {
            CustomRuntimeException exception = (CustomRuntimeException) throwable;
            log.error(exception);
        }
        return putObjectResult;
    }))
    .thenAccept(putObjectResult -> {
        if (putObjectResult != null) {
             FileUtils.deleteQuietly(file);
             log.debug("Deleted file {}", file.getName());
        }
    });

Spock test code: Spock测试代码:

@SpringBean
private S3Service s3service = Mock()

def "failed to upload article into s3"() {
    given: "mock the s3 service to throw CustomRuntimeException"
    s3Service.upload(_, _, _) >> {

        CompletableFuture<PutObjectResult> exception = new CompletableFuture<>();
        exception.completeExceptionally(new CustomRuntimeException())
        exception.exceptionally(new Function<Throwable, PutObjectResult>() {
            @Override
            PutObjectResult apply(Throwable throwable) {
                throw new CompletionException(throwable)
            }
        })

    }

Now when I debug the unit test cases the throwable instance in .handle is always null . 现在,当我调试单元测试用例时, .handlethrowable实例始终为null Also the same is happening when I mock the PutObjectResult 当我模拟PutObjectResult时也发生了同样的情况

So it seems my understanding of given and when are different from that of Mockito framework. 因此,似乎我对给定的理解以及何时与Mockito框架有所不同。 I had put that my s3Service.upload(_, _, _) in the then section and text cases works as expected. 我已经将我的s3Service.upload(_, _, _)放在then节和文本案例中可以正常工作。 So the final code is: 所以最终的代码是:

@SpringBean
private S3Service s3service = Mock()

def "failed to upload article into s3"() {
    given: "mock the s3 service to throw CustomRuntimeException"
    // given conditions
    when: "check here the with the actual beans"
    // actual bean calls
    then: "mention your mocks and calls"
    1 * s3Service.upload(_, _, _) >> {

        CompletableFuture<PutObjectResult> exception = new CompletableFuture<>();
        exception.completeExceptionally(new CustomRuntimeException())
        exception.exceptionally(new Function<Throwable, PutObjectResult>() {
            @Override
            PutObjectResult apply(Throwable throwable) {
                throw new CompletionException(throwable)
            }
        })

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM