I have the below code:
public final class JoinableTaskPool<T> extends ABC {
public Future<T> submit(final Callable<T> task) {
executorService.submit(new Runnable() {
public void run() {
try {
final Future<T> result = service.take();
try {
handler.processResult(result);
} catch (final Throwable t) {
throw new SearchException("Task has an error", t);
}
} catch (InterruptedException e) {
throw new SearchException("Task has an error", e);
}
}
}
}
}
I am trying to write a unit test for this method. Below is the method:
@RunWith(MockitoJUnitRunner.class)
public class TestJoinableTaskPool<T> {
private JoinableTaskPool<T> pool;
@Before
public void setUp() {
pool = new JoinableTaskPool<T>(1);
}
@After
public void tearDown() {
pool.shutdown();
}
@Test
public void testSubmit() throws Exception{
Callable task = (Callable<T>) () -> null;
Future<T> result = new FutureTask<T>(task);
Mockito.when(pool.getCompService().take())
.thenReturn(result);
Mockito.when(pool.getFinishHandler().processResult(result))
.thenThrow(RuntimeException.class);
pool.submit(task);
}
}
I am getting compilation error at below line saying:
reason: no instance(s) of type variable(s) T exist so that void conforms to T
Mockito.when(pool.getFinishHandler().processResult(result))
.thenThrow(RuntimeException.class);
How do we get around this? I have tried replacing T
as String
or any other class, but issue still exists. Passing any()
as parameter to processResult
does now work as well and I want that method call to throw an exception which assert on.
Any help would be appreciated.