如何使用mockito存根异步调用?

时间:2017-04-12 23:30:08

标签: java asynchronous mockito executorservice powermockito

假设我有两个类一起工作来执行这样的可调用:

public class blah {

@Autowired
private ExecutorServiceUtil executorServiceUtil;

@Autowired
private RestTemplate restClient;

public SomeReturnType getDepositTransactions(HttpHeaders httpHeaders) {

    ExecutorService executor = executorServiceUtil.createExecuter();
    try {
        DepositTransactionsAsyncResponse asyncResponse = getPersonalCollectionAsyncResponse( httpHeaders, executor);
        // do some processing 
        // return appropriate return type
    }finally {
        executorServiceUtil.shutDownExecutor(executor);
    }
}

Future<ResponseEntity<PersonalCollectionResponse>> getPersonalCollectionAsyncResponse( HttpHeaders httpHeaders, ExecutorService executor) {

    PersonalCollectionRequest personalCollectionRequest = getpersonalCollectionRequest(); // getPersonalCollectionRequest populates the request appropriately
    return executor.submit(() -> restClient.exchange(personalCollectionRequest, httpHeaders, PersonalCollectionResponse.class));
    }
}

public class ExecutorServiceUtil {

    private static Logger log = LoggerFactory.getLogger(ExecutorServiceUtil.class);

    public ExecutorService createExecuter() {
        return Executors.newCachedThreadPool();
    }

     public void shutDownExecutor(ExecutorService executor) {
            try {
                executor.shutdown();
                executor.awaitTermination(5, TimeUnit.SECONDS);
            }
            catch (InterruptedException e) {
                log.error("Tasks were interrupted");
            }
            finally {
                if (!executor.isTerminated()) {
                    log.error("Cancel non-finished tasks");
                }
                executor.shutdownNow();
            }
        }

}

如何使用Mockito存储响应并立即返回?

我已尝试过以下但我的innovcation.args()返回[null]

PowerMockito.when(executor.submit(Matchers.<Callable<ResponseEntity<OrxPendingPostedTrxCollectionResponseV3>>> any())).thenAnswer(new Answer<FutureTask<ResponseEntity<OrxPendingPostedTrxCollectionResponseV3>>>() {

            @Override
            public FutureTask<ResponseEntity<OrxPendingPostedTrxCollectionResponseV3>> answer(InvocationOnMock invocation) throws Throwable {
                Object [] args = invocation.getArguments();
                Callable<ResponseEntity<OrxPendingPostedTrxCollectionResponseV3>> callable = (Callable<ResponseEntity<OrxPendingPostedTrxCollectionResponseV3>>) args[0];
                callable.call();
                        return null;
                    }
                });

1 个答案:

答案 0 :(得分:1)

您可以在测试代码中使用ExecutorServiceUtil 来实现。我的意思是:您为生产代码提供该util类的模拟

该模拟确实返回了一个&#34;相同的线程执行器服务&#34 ;;而不是真正的服务&#34; (基于线程池)。编写这样一个同一线程的执行者实际上是直截了当的 - 见here

换句话说:您需要两个不同的单元测试:

  1. 您可以单独为ExecutorServiceUtil课程编写单元测试;确保它完成它应该做的事情(我认为:检查它返回一个非空的ExecutorService几乎已经足够了!)
  2. 您为使用模拟服务的blah类编写了单元测试。而且突然之间,你所有的问题都出现了#34;它是异步的&#34;走开;因为&#34; async&#34;部分在空气中消失。