问题描述
我试图理解Java EE 6提供的 EJBContainer
类与EJB模块的可嵌入单元测试和Arquillian之间的区别。
I am trying to understand the differences between the EJBContainer
class provided by Java EE 6 for embeddable unit testing of EJB modules and Arquillian.
是否有很好的资源,或者有人可以帮助我理解这一点?当我可以使用可嵌入容器测试EJB时,值得编写Arquillian测试吗?
Is there a good resource or can someone help me in understanding this? Is it worth to write Arquillian tests when I could test EJBs using an embeddable container?
推荐答案
完整披露:我是Arquillian
Full disclosure: I'm contributor of Arquillian
Arquillian是集成测试
的组件模型。
Arquillian is a component model for integration testing
.
通过使用 EJBContainer
,您将引入运行时(在本例中为容器)在您的测试中
。通过将运行时引入测试中,可以添加配置复杂性
。 Arquillian建立在相反的哲学
之上。 Arquillian做相反的事情,它将您的测试带到运行时
,因此它消除了测试带隙
(复杂性方面的差距)从单元测试过渡到集成测试)。
By using the EJBContainer
, you bring the runtime(the container in this case) in your tests
. By bringing the runtime in your tests, you add configuration complexity
to them. Arquillian is built on the opposite philosophy
. Arquillian does the opposite thing, it brings your test to the runtime
and thus it eliminates the testing bandgap
(gap in complexity while moving from unit to integration testing).
您将在以下示例中意识到上述差异。
You will realize the above mentioned difference in the below examples.
使用Arquillian测试EJB:
Test an EJB using Arquillian:
@RunWith(Arquillian.class)
public class ArquillianGreeterTest {
@Deployment
public static JavaArchive createTestArchive() {
return ShrinbkWrap.create("greeterTest.jar", JavaArchive.class)
.addClasses(Greeter.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
}
@EJB private Greeter greeter;
@Test
public void testGreeting() {
assertNotNull(greeter);
assertEquals("Welcome to the Arquillian Universe :)", greeter.greet());
}
}
使用EJBContainer的同一示例将看起来像:
The same example using EJBContainer, would look like:
public class EJBContainerGreeterTest {
private static EJBContainer ejbContainer;
private static Context ctx;
@BeforeClass
public static void setUpClass() throws Exception {
ejbContainer = EJBContainer.createEJBContainer();
ctx = ejbContainer.getContext();
}
@AfterClass
public static void tearDownClass() throws Exception {
if (ejbContainer != null) {
ejbContainer.close();
}
}
@Test
public void testGreeting() {
Greeter greeter = (Greeter)
ctx.lookup("java:global/classes/Greeter");
assertNotNull(greeter);
assertEquals("Welcome to the Arquillian Universe :)", greeter.greet()) ;
}
}
我希望这会有所帮助。
这篇关于Arquillian vs EJB可嵌入容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!