java-使用IgniteFuture进行单元测试

时间:2018-08-14 07:39:06

标签: java junit mockito ignite

在我的代码中,我有以下内容:

// for the test:
// executor is an ExecutorService that will run in the current thread
// compute is an IgniteCompute (can be mocked)

String other = "blah";
IgniteFuture<String> future = compute.callAsync(callable).chainAsync(i -> myCreate(i, other), executor);

方法myCreate是该类中的私有方法,我想确保对它进行单元测试。我尝试模拟IgniteCompute,但是callAsyncchainAsync的结果都被模拟,导致我的方法没有被调用。关于如何获得真正的myCreate方法以在运行上述代码的测试中运行的任何想法?

2 个答案:

答案 0 :(得分:2)

您可以在嵌入式内存模式下启动Ignite进行测试。要启动节点,请使用Ignition.start(...)方法。

点火节点非常轻巧,不需要很多资源即可启动。我认为,这比模拟IgniteCompute更加容易和透明。

答案 1 :(得分:0)

在测试异步内容时,通常将必须捕获发送到异步的参数,然后在测试中执行它,然后验证发送到异步的方法的结果。像这样:

final ArgumentCaptor<IgniteClosure> closureCaptor = ArgumentCaptor.forClass(IgniteClosure.class);
when(computeMock.callAsync(any(IgniteCallable.class))).thenReturn(futureMock);
when(futureMock.chainAsync(any(IgniteClosure.class), any(Executor.class))).then(invocation -> null);

//invoke method under test

verify(futureMock).chainAsync(closureCaptor.capture(), any(Executor.class));

closureCaptor.getValue().apply(executorMock);
//verify return value, other invocations, etc.
相关问题