I have some service which uses an Java EE ManagedExecutorService
(in Wildfly 9)
public class FooService{
@Resource
ManagedExecutorService executorService;
}
For a test with Mockito, I would like to use a "normal" ExecutorService
@RunWith(MockitoJUnitRunner.class)
public class FooServiceTest{
@Spy
ManagedExecutorService executorService = Executors.newFixedThreadPool(5);
}
This code obviously does not compile, as an ExecutorService
is not a ManagedExecutorService
.
When using an ExecutorService
in the service, the test runs without errors, but then Wildfly cannot inject the service.
public class FooService{
@Resource
ExecutorService executorService;
}
@RunWith(MockitoJUnitRunner.class)
public class FooServiceTest {
@Spy
ExecutorService executorService = Executors.newFixedThreadPool(5);
}
It would be possible to create a ManagedExecutorService
by delegating to a ExecutorService
:
@Spy
ManagedExecutorService executorService = new ManagedExecutorService() {
ExecutorService executorService = Executors.newFixedThreadPool(5);
@Override
public void shutdown() {
executorService.shutdown();
}
// delegate for all (12) methods
}
Is there a simpler way to use an ExecutorService
in the test without changing the service which runs in Wildfly?