Java Reactor + Caffeine Caching + Mockito =严格禁止参数不匹配

mrwjdhj3  于 5个月前  发布在  Java
关注(0)|答案(1)|浏览(87)

我在一个Spring Boot项目中使用Java Reactor,我需要在中间步骤中缓存我的数据。它在Spring Boot 3,Java 17,Junit5.
我的缓存服务看起来像这样:

@Component
public class CachingService {
  private static final int DURATION = 60;

  public Mono<Resource> ofMono(final UUID guid, final Mono<Resource> mono) {
    final String cachingKey = "key";
    final Function<String, Mono<Resource>> monoFn = ofMono(key -> mono);
    return Mono.defer(() -> monoFn.apply(cachingKey));
  }

  private Function<String, Mono<Resource>> ofMono(final Function<String, Mono<Resource>> fn) {
    final AsyncLoadingCache<String, Resource> cache =
        Caffeine.newBuilder()
            .expireAfterWrite(Duration.ofSeconds(DURATION).multipliedBy(2))
            .refreshAfterWrite(Duration.ofSeconds(DURATION))
            .buildAsync((k, e) -> fn.apply(k).subscribeOn(Schedulers.fromExecutor(e)).toFuture());

    return (k) -> Mono.fromFuture(cache.get(k));
  }
}

字符串
所以我的目的是缓存一个“Resource”记录的示例,它从另一个服务发布为Mono,并将再次在另一个服务中使用。
我在另一个服务中注入cachingService并调用它的方法:

@Override
  public Mono<Resource> getResourceWithAccess(final UUID guid) {
    final Mono<Boolean> viewAccess = accessService.getViewAccess(guid);

    return viewAccess
        .flatMap(hasAccess -> mapToResource(guid, hasAccess))
        .flatMap(resource -> cachingService.ofMono(guid, Mono.just(resource)));
  }


代码中使用的accessService.getViewAccess和mapToResource:

Mono<Boolean> accessService.getViewAccess(UUID fileGuid);

  Mono<Resource> mapToResource(
      final UUID guid, final Boolean hasAccess);


我有一个这样的Mockito测试:

@Test
  void test() {
    // GIVEN
    ...
    private ResourceWithAccessService service
    @Mock private CachingService cachingService;

    Resource expectedResource = ...
    when(cachingService.ofMono(fileGuid, Mono.just(expectedResource ))).thenReturn(Mono.just(expectedResource ));

    // WHEN
    final Mono<Resource > actualResource =
        service.getResourceWithAccess(fileGuid);

    // THEN
    StepVerifier.create(actualResource).expectNext(expectedResource).verifyComplete();
  }


不幸的是,我得到了这个错误:

java.lang.AssertionError: expectation ....
actual: onError(org.mockito.exceptions.misusing.PotentialStubbingProblem: 
Strict stubbing argument mismatch. Please check:
 - this invocation of 'ofMono' method:
    cachingService.ofMono(
    98f0b409-20b9-3674-a0e6-eb5048219f2c,
    MonoJust
);
    -> at com...lambda$getResourceWithAccess$1(ResourceWithAccessServiceImp.java:52)
 - has following stubbing(s) with different arguments:
    1. cachingService.ofMono(
    98f0b409-20b9-3674-a0e6-eb5048219f2c,
    MonoJust
);

czfnxgou

czfnxgou1#

我终于找到了一个解决办法。通过使用ArgumentCaptor,我仍然能够检查我的方法是否被正确的参数调用。

private void verifyCachingService(final UUID fileGuid, final Resource resource) {
    final ArgumentCaptor<Mono> acMono = ArgumentCaptor.forClass(Mono.class);
    verify(cachingService).ofMono(eq(fileGuid), acMono.capture());
    StepVerifier.create(acMono.getValue()).expectNext(resource).verifyComplete();
  }

字符串

相关问题