gpt4 book ai didi

java - 使用 stub 和 openejb 框架测试 ejb

转载 作者:太空宇宙 更新时间:2023-11-04 08:41:09 24 4
gpt4 key购买 nike

我正在尝试测试一个已注入(inject)另一个 EJB 的 EJB。出于测试目的,我想对注入(inject)的 EJB 使用 stub 。我使用 openEJB 作为 EJB 的框架进行测试。

这是 EJB:

@Stateless
@Local(IService.class)
public class Service implements IService {

@EJB
private IBean bean;

@Override
public String doService(String data) {
return bean.process(data);
}
}

真正注入(inject)的EJB:

@Stateless
@Local(IBean.class)
public class Bean implements IBean {

private static Logger logger = Logger.getLogger(Bean.class);

@Override
public String process(String data) {
logger.info("Bean processing : " + data);
return "Bean processing : " + data;
}
}

EJB 的 stub 版本:

@Stateless
@Local(IBean.class)
public class BeanStub implements IBean {

private static Logger logger = Logger.getLogger(BeanStub.class);

@Override
public String process(String data) {
logger.info("Stub processing : " + data);
return "Stub processing : " + data;
}
}

以及使用的 JUnit 测试:

public class ServiceTest {

private static Logger logger = Logger.getLogger(ServiceTest.class);

private static InitialContext context;

@BeforeClass
public static void setUpBeforeClass() throws Exception {
// openEJB
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY,"org.apache.openejb.client.LocalInitialContextFactory");
p.put("openejb.altdd.prefix", "stub"); // use specific ejb-jar
p.put("openejb.descriptors.output", "true");

context = new InitialContext(p);
}

@Test
public void testServiceStub() {
try {
IService service = (IService) context.lookup("ServiceStubLocal");
assertNotNull(service);
String msg = service.doService("service");
assertEquals("Stub processing : service", msg);
} catch (NamingException e) {
logger.error(e);
fail(e.getMessage());
}
}
}

我尝试使用特定的 ejb-jar 来覆盖 stub EJB 对真实 EJB 的使用(我想在我的服务中使用“BeanStub”而不是默认的“Bean”):

 <ejb-jar>
<enterprise-beans>
<session id="ServiceStub">
<ejb-name>ServiceStub</ejb-name>
<ejb-class>tests.Service</ejb-class>
<ejb-local-ref>
<ejb-ref-name>tests.Service/bean</ejb-ref-name>
<ejb-link>BeanStub</ejb-link>
</ejb-local-ref>
</session>
</enterprise-beans>
</ejb-jar>

不幸的是,我在声明 EJB 时遇到了问题:

    Apache OpenEJB 3.1.4    build: 20101112-03:32http://openejb.apache.org/17:14:29,225  INFO startup:70 - openejb.home = D:\Workspace_Java\tests\testejb17:14:29,225  INFO startup:70 - openejb.base = D:\Workspace_Java\tests\testejb17:14:29,350  INFO config:70 - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service)17:14:29,350  INFO config:70 - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager)17:14:29,381  INFO config:70 - Found EjbModule in classpath: D:\Workspace_Java\tests\testejb\target\test-classes17:14:29,412  INFO config:70 - Found EjbModule in classpath: D:\Workspace_Java\tests\testejb\target\classes17:14:29,428  INFO config:70 - Beginning load: D:\Workspace_Java\tests\testejb\target\test-classes17:14:29,428  INFO config:70 - AltDD ejb-jar.xml -> file:/D:/Workspace_Java/tests/testejb/target/test-classes/META-INF/stub.ejb-jar.xml17:14:29,850  INFO config:70 - Beginning load: D:\Workspace_Java\tests\testejb\target\classes17:14:29,850  INFO config:70 - AltDD ejb-jar.xml -> file:/D:/Workspace_Java/tests/testejb/target/classes/META-INF/stub.ejb-jar.xml17:14:29,850  INFO config:70 - Configuring enterprise application: classpath.ear17:14:29,912  INFO config:70 - Configuring Service(id=Default Stateless Container, type=Container, provider-id=Default Stateless Container)17:14:29,912  INFO config:70 - Auto-creating a container for bean ServiceStub: Container(type=STATELESS, id=Default Stateless Container)17:14:29,912  INFO options:70 - Using 'openejb.descriptors.output=true'17:14:29,912  INFO options:70 - Using 'openejb.descriptors.output=true'17:14:29,928  INFO config:70 - Dumping Generated ejb-jar.xml to: C:\TEMP\ejb-jar-6391test-classes.xml17:14:29,959  INFO config:70 - Dumping Generated openejb-jar.xml to: C:\TEMP\openejb-jar-6392test-classes.xml17:14:29,959  INFO options:70 - Using 'openejb.descriptors.output=true'17:14:29,959  INFO config:70 - Dumping Generated ejb-jar.xml to: C:\TEMP\ejb-jar-6393classes.xml17:14:29,975  INFO config:70 - Dumping Generated openejb-jar.xml to: C:\TEMP\openejb-jar-6394classes.xml17:14:30,006  INFO config:70 - Enterprise application "classpath.ear" loaded.17:14:30,084  INFO startup:70 - Assembling app: classpath.ear17:14:30,131  INFO startup:70 - Jndi(name=ServiceStubLocal) --> Ejb(deployment-id=ServiceStub)17:14:30,131 ERROR startup:46 - Jndi name could not be bound; it may be taken by another ejb.  Jndi(name=openejb/Deployment/ServiceStub/tests.IService!Local)17:14:30,131  INFO startup:70 - Undeploying app: classpath.ear17:14:30,147 ERROR startup:50 - Application could not be deployed:  classpath.earorg.apache.openejb.OpenEJBException: Creating application failed: classpath.ear: Unable to bind business local interface for deployment ServiceStub    at org.apache.openejb.assembler.classic.Assembler.createApplication(Assembler.java:679)    at org.apache.openejb.assembler.classic.Assembler.createApplication(Assembler.java:450)

方法或编写 ejb-jar 的方式有问题吗?

最佳答案

我在使用 OpenEJB 时也遇到了类似的问题和障碍。如果您需要对测试进行 stub 和模拟(谁不需要),请看看我最终设法处理它的人(在 OpenEJB 联合创始人 David 的大力帮助下)。在最新版本(3.1.4)中,OpenEJB 的工作方式与 Arquillian 非常相似,允许内部类测试驱动程序,无需 ejb-jar.xml 和类路径扫描。

我在这里描述了我的障碍:http://jakub.marchwicki.pl/posts/2011/07/01/testing-ejb-application-openejb-without-classpath-scanning/ 。看一下,也许这会让你的测试更容易。

关于java - 使用 stub 和 openejb 框架测试 ejb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5275487/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com