Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WildFly: How to test EJB using embedded container?

I'm doing my first Java EE project and I want to preform a test. I searched and found that since EJB 3.1, there's the possibility of using an embedded EJB container to test the business layer. I'm using WildFly but I haven't found how to configure the embedded container.

So, how to configure the embedded container properly and test EJBs 3.1+ with WildFly?

I appreciate any help!

like image 732
codenoob Avatar asked May 11 '15 13:05

codenoob


1 Answers

You must use framework arquillian from jboss itself http://www.mastertheboss.com/jboss-frameworks/arquillian/arquillian-tutorial.

What this framework does is this: in the background the instance of jboss is created. Everything is deployed there so you can then run your ejb's or servlets on that "background" container.

It is not easy to configure it, so I would recommend you to start with Jboss Tools for eclipse. Code looks then like this:

@Deployment
public static Archive<?> createTestArchive() {
     return ShrinkWrap.create(WebArchive.class, "test-demo.war")
            .addAsResource("META-INF/persistence.xml")
            .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
}

I do not favor this approach, it's really a separate deployment, you need to understand fully the structure of WAR and EJB packages and all other details.

It's really better, that you your EJB's , JPAs and Servlet beans are just light wrappers around normal java classes (POJO's), where the real logic resides. Then you can use plain unit tests to test those.

like image 65
Mitja Gustin Avatar answered Oct 02 '22 14:10

Mitja Gustin