Testing of JavaEE Applications with Arquillian

Consider following trivial JavaEE application:

arquillian-prolog2

Sample JavaEE application with module FooBean we want to test

We want to test FooBean. However FooBean uses database or a distributed cache (e.g. Infinispan) or even another EJB bean. I.e. it depends on expensive component.

Options:

  1. units tests with mocking of expensive components;
  2. integration tests via FooService.

Option #1 is weak because mocking itself is tedious. Option #2 is weak because it could bring inadequate widening of FooService  – all interesting methods of  FooBean must get exposed somehow as JAX-RS resources.

Another approach is too use Arquillian for testing. It combines options #1 and #2 but is free of their weaknesses. Here is its main idea:

  • Craft test artifact (e.g. WAR file). Include into this artifact the JavaEE component we want to test (e.g. FooBean)
  • Write test class as it were an usual unit test. Inside test class use @EJB / @Inject annotations to link tested modules (e.g. @EJB Foo foo). Call methods of linked module inside @Test methods
  • Include written test class into a temporary artifact as well
  • Deploy resulting test artifact to a real container and execute test inside of this container

Below is an example of a test Arquillian class and resulting test artifact (EAR test-foo-ejb.ear in our case):

  • tested FooBean  (along with its interface Foo) lives inside of foo-ejb.jar, the latter is included into test artifact as is;
  •   FooTest  class is included into test-foo-ejb.war and is included into test artifact as well.
arquillian-code-artifact3

Correspondence between Arquillian test class and artifact (for deployment)

Running mvn verify  or mvn test  (depending on whether this test is of unit or integration type) will result in:

  • crafted test artifact is deployed to container;
  • since this is a real container than all necessary dependencies of a component (e.g. database) are satisfied in a natural way without any mocking or simulation;
  • code inside of test class is launched by Arquillian. Code is executed inside a container with full access to a tested component!
  • after test is executed test artifact is undeployed from container.
arquillian-mvn-verify

Deployments in container during Arquillian test execution

Thanks to modules isolation inside of an application container  coexistence of the same FooBean inside two different EARs (real-foo.ear and crafted test-foo-ejb.ear) is possible. The same time all relevant dependencies (e.g. on database) are retained and this gives an ability to touch and call  FooBean in any way we want from FooTest.

 

 

Leave a Reply

Your email address will not be published.