WildFly: How to test EJB using embedded container?
Asked Answered
H

1

8

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!

Headless answered 11/5, 2015 at 13:52 Comment(4)
Are you looking for Arquillian, the Integration Testing framework for Wildfly? Or are you looking for what's been discussed in the Stackoverflow question about embedding the EJB Container in Unit Tests?Oquinn
Hi @fxnn, that would be the second option, I have found many posts with the same code, but it appears that it has a differente setup depending on the AS you're using, in my case it is Wildfly.Headless
You could use the wildfly-maven-plugin to start the server during tests and shut it down when tests are complete.Maple
What do you mean by "embeded" EJB container? Did the wildfly-maven-plugin work for you? I've been using the analogous plugin for jboss as for my nightly builds and it has worked magnificently!Nonlinearity
D
5

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.

Daub answered 18/5, 2016 at 7:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.