Skip to content
Advertisement

junit test for stateless ejb + jpa

I would like to write a junit test for my stateless ejb + jpa demo code. I think it is actually not a junit test, it is an integration test.

I have a stateless ejb with an injected EntityManager and PostgreSQL database server is used. I use CDI (Spring is not used in my project) and EclipseLink with a persistent.xml file. My application will be executed on GlassFish server.

I would like to write a test which checks the full logic: calls a method on my example stateless ejb and persist data into a in-memory database. I want to start the in-memory database with my tests and stop it when my test class was executed.

ejb class:

@Stateless
public class PropertyServiceImpl implements PropertyService {

    @PersistenceContext(name = "anything-jndi-em")
    private EntityManager em;

    public String getStringValue(final String key) {
        Property property = em.createNamedQuery("Property.findByKey", Property.class)
                .setParameter("key", key)
                .getSingleResult();

        return property.getValue();
    }
}

enitity class:

@Entity
@Table(name = "APPLICATION_SETTING")
@NamedQueries({
        @NamedQuery(name = "Property.findByKey", query = "select a from Property a where a.key = :key and a.status = 1")
})
public class Property
{
    @Id
    @SequenceGenerator(name = "APPLICATION_SETTING_SEQ", sequenceName = "APPLICATION_SETTING_SEQ", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "APPLICATION_SETTING_SEQ")
    @Column(name = "ID", unique = true, nullable = false)
    private Long id;

    @Column(name = "KEY", length = 200, nullable = false)
    private String key;
    ...
}

If I am correct I need to follow the next steps:

  1. create a new persistent.xml file with the proper jdbc connection parameters which will connect to the in-memory dadabase and put it under the /test/Resources/META-INF folder
  2. add some pom dependencies for in-memory database (ex.: hsqldb) and embedded ejb container
  3. create a simple PropertyServiceImplTest.java class
  4. configure somehow that the /test/Resources/META-INF/persistent.xml file will be user by my test class
  5. initialize the embedded ejb container and start the in-memory database
  6. execute my juni test method:

@Test public void testGetStringValue() { PropertyService service = new PropertyServiceImpl(); assertNotNull(service.getStringValue("abc")); }

Could you please help my to write a proper test java class for this scenario?

Advertisement

Answer

In your actual test case boot up the javax.ejb.embeddable.EJBContainer. After that use its javax.naming.Context to lookup your stateless bean. The you can use your bean like you are used to and assert its behavior. Keep in mind that an embeddable container impl only has to support a subset (ejb lite) of functionality compared to a full blown ejb container. Here you find a pretty neat example.

Code snippet:

JBContainer ejbContainer = EJBContainer.createEJBContainer();
Context ctx = ejbContainer.getContext();
PropertyService  service = (PropertyService) ctx.lookup("java:global/classes/PropertyServiceImpl");
assertNotNull(service.getStringValue("abc"));
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement