Tags

, , ,


JBoss Arquillian is an awesome tool for integration testing of Java EE applications brought by JBoss team to the community. It enables real in-container testing with no mocks, and writing the tests is made very simple, just like if you would write ordinary unit tests.

Arquillian wraps a lifecycle around test execution that provides the following services:

  • Manages the lifecycle of one or more containers
  • Bundles the test case, dependent classes and resources as Shrink-wrap archives
  • Deploys the archives to the containers
  • Enriches the test case with dependency injection and other declarative services
  • Executes the tests inside (or against) the containers
  • Returns the results to the test runner for reporting

Create a new Maven Project with Group Id=org.jboss.weld.archtypes & Artifact id=jboss-javaee6-webapp

write a testcase for a CDI injectable Bean/EJB

import static org.junit.Assert.assertEquals;

import javax.ejb.EJB;

import org.jboss.arquillian.container.test.api.Deployment;

import org.jboss.arquillian.junit.Arquillian;

import org.jboss.shrinkwrap.api.ShrinkWrap;

import org.jboss.shrinkwrap.api.spec.JavaArchive;

import org.junit.Test;

import org.junit.runner.RunWith;

 

@RunWith(Arquillian.class)

public class CalculatorTest {

    @Deployment

    public static JavaArchive createDeployment() {

        return ShrinkWrap.create(JavaArchive.class) //define the type of bundle

            .addClass(Calculator.class) //bundle any class or resource you want

            .addAsManifestResource(EmptyAsset.INSTANCE, “beans.xml”);//You need bean.xml to activate CDI in your app

    }

 

    @Inject

    Greeter greeter;

 

    @Test

    publicvoid should_create_greeting() {

        assertEquals(5,Calculator.sum(2,3));

    }

}

Run the test

Wow! You did it without writing a single line of code to get & manage the context Application Server

Is not it truly painless?

Advertisements