简体   繁体   中英

java - Unit test with IgniteFuture

In my code, I have the following:

// 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);

The method myCreate is a private method in the class that I would like to ensure gets unit tested. I tried mocking the IgniteCompute but then the result of callAsync and chainAsync both get mocked resulting the in my method not getting called. Any ideas on how I can get the real myCreate method to run in a test that runs the above line?

You can start Ignite in embedded in-memory mode for testing. To start a node use Ignition.start(...) method.

Ignite nodes are pretty lightweight and don't require much resources to start. I think, it's easier and more transparent than mocking IgniteCompute.

When testing async stuff you will typically have to catch the argument sent to async, then execute it in the test, and then verify the result of the method sent to async. So something like this:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM