I am trying to find a portable solution to test my Java EE 7 application. It is especially tricky when to test the EJB and their injections. For example:
@org.junit.Test
public void testIsValidCredentials() throws Exception {
System.out.println("isValidCredentials");
String username = "";
String password = "";
Map properties = new HashMap();
properties.put(EJBContainer.MODULES, new File[] {new File("target/classes")});
EJBContainer container = javax.ejb.embeddable.EJBContainer.createEJBContainer();
AuthenticatorLocal instance = (AuthenticatorLocal) container.getContext().lookup("java:global/classes/Authenticator");
boolean expResult = false;
boolean result = instance.isValidCredentials(username, password);
assertEquals(expResult, result);
container.close();
}
When I run test I will get:
No EJBContainer provider available
I also tried to use the option properties.put(EJBContainer.PROVIDER, "")
, but no success. There is some documentation available for Glassfish, but for Wildfly it is really poor.
Also I have heard of arquillian, but I only see Alpha packages, which doesn't seem production safe. Does anyone know a portable solution for (integration) testing?
Here is a fairly standard approach that I use to execute JUnit tests in a Java EE environment.
The basic idea is:
Here is an illustration of what that might look like:
Use a JUnit SE suite with basic test stubs to drive GET requests to your application:
Use corresponding methods in a test servlet to execute the real test in a Java EE environment:
There are third party libs you can use to make this a little more simple, but this is the basic idea. The examples I showed here can all be done with standard Java libraries (and a Java EE environment, of course). The only stuff in here that is specific to what app server you are running on is the server start and stop logic.
Also, I've heard good things about Arquillian, but last I heard it's only supported for a select few Java EE app servers, which would break your portability requirement.