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.
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?
Full disclosure: I'm contributor of Arquillian
Arquillian is a component model for integration testing
.
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.
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());
}
}
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()) ;
}
}
I hope this helps.
Arquillian eventually builds on top of these ideas. Think of it as an empty placeholder, a car, that has no engine.But it can accept different engines, e.g GlassFishEngine, Jboss Engine, Websphere Engine. So you use arquillian, you define the engine of your choice, you place as passengers your code to be tested, and you put the car for a ride.
I have rewritten a series of post for newbies regarding Arquillian so if you find the first sections relevant to your question. You can find it here here
来源:https://stackoverflow.com/questions/24310912/arquillian-vs-ejb-embeddable-container