- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一些使用 Java EE 的服务 ManagedExecutorService
(在 Wildfly 9 中)
public class FooService{
@Resource
ManagedExecutorService executorService;
}
对于 Mockito 的测试,我想使用“正常”ExecutorService
@RunWith(MockitoJUnitRunner.class)
public class FooServiceTest{
@Spy
ManagedExecutorService executorService = Executors.newFixedThreadPool(5);
}
这段代码显然无法编译,因为 ExecutorService
不是 ManagedExecutorService
。
在服务中使用 ExecutorService
时,测试运行没有错误,但 Wildfly 无法注入(inject)服务。
public class FooService{
@Resource
ExecutorService executorService;
}
@RunWith(MockitoJUnitRunner.class)
public class FooServiceTest {
@Spy
ExecutorService executorService = Executors.newFixedThreadPool(5);
}
可以通过委托(delegate)给 ExecutorService
来创建 ManagedExecutorService
:
@Spy
ManagedExecutorService executorService = new ManagedExecutorService() {
ExecutorService executorService = Executors.newFixedThreadPool(5);
@Override
public void shutdown() {
executorService.shutdown();
}
// delegate for all (12) methods
}
有没有更简单的方法在测试中使用 ExecutorService
而无需更改在 Wildfly 中运行的服务?
最佳答案
我使用注入(inject)模式:
class DefaultImpl {
static DefaultFactory me;
static DefaultFactory getCurrentInstance()
{ if (me == null) {
me = new DefaultFactory();
}
return me;
}
void setCurrentInstance(DefaultFactory df){
me = df;
}
ExecutorService getExecutorService(){
// lookup and return ManagedExecutorService
}
}
class TestImpl extends DefaultImpl{
TestImpl(){
DefaultFactory.setCurrentInstance(this); <-- this now sets your TestImpl for any call
}
ExecutorService getExecutorService(){
ExecutorService executorService = Executors.newFixedThreadPool(5);
return executorService;
}
}
//junit
@Test
void someTest(){
new TestImpl();
// do something all your calls will use non managed executor service
}
您还可以从 junit setup() 调用 new TestImpl()。另外,如果你想让它更灵活,那么你可以有一个 BaseImpl 并让 DefaultImpl 和 TestImpl 扩展它。
希望这对您有所帮助!
关于java - 在测试中用 ExecutorService 替换 ManagedExecutorService,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37722360/
我对 Arquillian 和 ManagedExecutorService 存在问题。 Arquillian 无法找到默认的 ManagedExecutorService。异常(exception)
我正在使用ManagedExecutorService处理作业列表。我在主线程中处理作业并使用 ManagedExecutor 执行每个作业。将来作业大小有可能超过池大小...所以我想知道,发生这种情
类 javax.enterprise.concurrent.ManagedExecutorService 管理 Enterprise Java Bean 中的线程。 As threads in EJB
我想添加新的ManagedExecutorService资源到 WildFly 10。 我编辑standalone.xml,并将行添加到 : 重新启动wildfy后,我在 JNDI View 中找
我有一些使用 Java EE 的服务 ManagedExecutorService (在 Wildfly 9 中) public class FooService{ @Resource
在描述我的问题之前,我想概述一下我希望通过我的应用程序实现的目标。 我基本上想下载可能有大量文件,并定义最大并行下载数。 我想在应用程序服务器上运行此应用程序并为此选择 Glassfish 4(我想尝
在ManagedExecutorService的javadoc中,有 Since: 1.0 这个接口(interface)不是来自 jsr 2362013 年发布的? 这不是 JavaEE 7 的新功
在 Java SE 中,我们可以使用像 这样的结构 ExecutorService es1 = Executors.newSingleThreadExecutor(); ExecutorService
我正在尝试学习如何在 wildfy 11 上使用托管执行程序服务,但是当我部署应用程序时,出现以下错误: 10:28:34,185 错误 [org.jboss.as.controller.manage
我们当前的旧版 Web 应用程序在其中创建不受应用程序服务器容器管理的线程。我必须用JavaEE多线程标准来修改它。 我的网络应用程序在 Tomcat 上运行良好,但在 Websphere 上失败。
我最近使用 GlassFish/Payara 构建了一个 Java EE 7 Web 应用程序。 Web 应用程序在底层操作系统上按需启动长时间运行的任务(> 2 小时)。这些任务基本上是用 Pyth
我在 REST 服务中调用了以下方法,需要立即返回: @Inject private ScheduleManager scheduleManager; @Resource(name = "Defaul
我是 Vaadin 框架和 Java EE 的新手。我正在使用 Vaadin Framework 8.1.6,Vaadin-CDI 插件 2.0.0,在 TomEE 7.0.2 服务器上运行。我正在尝
我是一名优秀的程序员,十分优秀!