class java.util.LinkedList 不能强制转换为类 org.reactivestreams.Publ



我正在尝试使用Junit和Mockito创建一个单元测试。但是我收到一个错误说

class java.util.LinkedList cannot be cast to class org.reactivestreams.Publisher

当它清楚地返回字符串列表时。

测试

@Test
public void fullLoad() {
when((Publisher<?>) this.mockedProductComponent.getErpNumbers("US", "es")).thenReturn(
just(new ArrayList<>())
);
}

正在测试的方法

public boolean fullLoad(String country, String language) {
List<String> erpNumbers = productComponent.getErpNumbers(country, language);
log.info("Retrieved following ERP numbers: {}", erpNumbers);
Lists.partition(erpNumbers, batchSize)
.forEach(handleBatch(country, language));
return true;
}

试图模拟的方法

public List<String> getOnlineErpNumbers(String country, String lang) {
return webClient.get()
.uri(uriBuilder -> uriBuilder
.scheme(scheme)
.host(host)
.path(path)
.port(port)
.pathSegment(country)
.pathSegment(lang)
.pathSegment("erp")
.build())
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.bodyToMono(new ParameterizedTypeReference<List<String>>(){})
.timeout(Duration.ofMillis(3000))
.onErrorReturn(Collections.emptyList())
.block();
}

我知道这一定是因为我以被动的方式执行此操作,但我无法找到有关如何测试此类过程的文档。

你混淆了你的导入。

您正在使用来自 Reactor 的 Mono.when,它接受发布者:

public static Mono<Void> when(Publisher<?>... sources)

而不是莫基托。

public static <T> OngoingStubbing<T> when(T methodCall)

相关内容

最新更新