简体   繁体   English

EJB3无状态会话Bean是否允许继承?

[英]Is inheritance allowed for EJB3 Stateless Session Beans?

Suppose I have a stateless bean: 假设我有一个无状态bean:

@Stateless(name = "fooBean")
@LocalBean
public class Foo {
...
}

I want to create a mock for testing (with CDI) 我想创建一个用于测试的模拟(使用CDI)

@Alternative @Specializes
public class FooMock extends Foo {
...
}

I register the mock in beans.xml , and when I run it with OpenEJB, I get the following exception: 我在beans.xml注册了mock,当我用OpenEJB运行它时,我得到以下异常:

...
Caused by: javax.naming.NameAlreadyBoundException:
openejb/Deployment/fooBean/com.company.Foo!LocalBeanHome

It seems to me that the container creates an interface for Foo because of @LocalBean which is now implemented by FooMock , too. 在我看来,容器为Foo创建了一个接口,因为@LocalBean现在也由FooMock实现。 OpenEJB tries to bind the two classes with the with same name of same generated interface, which is not possible. OpenEJB尝试使用相同生成的接口的相同名称绑定这两个类,这是不可能的。

Of course, if I comment out @Stateless and @LocalBean in Foo class, it works fine. 当然,如果我在Foo类中注释掉@Stateless@LocalBean ,它运行正常。

Any idea or suggestion to solve this? 有任何想法或建议来解决这个问题吗?

Rather do something like: 而是做类似的事情:

@Local
public interface Foo {
...
}

@Stateless
public class FooBean implements Foo {
...
}

Then make your mock. 然后做你的模拟。

@Alternative
@Specializes
public class FooMock implements Foo {
...
}

However I recommend you take a look at Arquillian it will make your testing so much easier without the need for @Alternative and special beans.xml entries for tests. 但是我建议你看看Arquillian它会让你的测试变得如此简单,而不需要@Alternative和特殊的beans.xml条目进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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