Arquillian vs EJB embeddable container

笑着哭i 提交于 2019-12-07 16:29:22

问题


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?


回答1:


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.




回答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



来源:https://stackoverflow.com/questions/24310912/arquillian-vs-ejb-embeddable-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!