Arquillian vs EJB embeddable container
Asked Answered
T

2

3

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?

Turney answered 19/6, 2014 at 15:51 Comment(2)
Thanks for updating the grammer Arjan. Is there an answer on this that you could help out on?Turney
EJBContainer is specific for EJBs. Let's say your application runs on GlassFish, how would you get the EJBContainer? How would you test things like REST endpoints or JSF controllers? Arquillian is more full spectrum than just testing EJBs. Maybe if you provided more of your use case it would easier to answer.Cloud
B
5

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.

Boy answered 9/7, 2014 at 11:12 Comment(0)
B
2

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

Blather answered 29/6, 2014 at 10:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.