Mockito如何将类型传递给返回CompleteableFuture的通用supplyAsync方法?

时间:2018-08-14 09:32:49

标签: java concurrency mocking mockito powermock

假设我有一个执行人:

public class ExecutorUtil {
private ExecutorService executorService;

public <T> CompletableFuture<T> supplyAsync(Supplier<T> supplier) {
    return CompletableFuture.supplyAsync(supplier, executorService);
}
}

并调用通用的supplyAsync方法,例如:

public CompletableFuture<List<String>> getBuryListFuture(){
    CompletableFuture<List<String>> buryFuture = executorUtil.supplyAsync( () -> {
        List<String> burySkus = null;
        try {
            System.out.println("Here bury:");
            burySkus = ruleEngine.getBuriedSkus();
        } catch (Exception e) {
            LOGGER.info("[ERROR]: There is some issue with RuleEngine");
        }
        return burySkus;
    });
    return buryFuture;
}

和:

public<Source> CompletableFuture<List<StrategyDTO>> getStrategyListFuture(Source source){

    CompletableFuture<List<StrategyDTO>> strategyFuture = executorUtil.supplyAsync( () -> {
        List<StrategyDTO> strategyList;
        try {
            strategyList = recommendationService.execute(source).getStrategies();
            strategyList = removeDuplicates(strategyList);
        } catch (OrchestrationException e) {
            throw new CompletionException(e);
        }
        return strategyList;
    });
    return strategyFuture;
}

对于每个这些调用,如何让Mockito模拟执行者的不同响应?到目前为止,我只能为两者模拟相同的响应。

使用:

CompletableFuture<Object> future3 = new CompletableFuture<>();
future3.complete(recResponse.getStrategies());
PowerMockito.when(executorUtil.supplyAsync(Matchers.any())).thenReturn(future3);

尝试区分:

PowerMockito.when(executorUtil.supplyAsync(Matchers.<Supplier<List<String>>>any())).thenReturn(future4);
    PowerMockito.when(executorUtil.supplyAsync(Matchers.<Supplier<List<StrategyDTO>>>any())).thenReturn(future3);

但这没什么区别。

0 个答案:

没有答案