« Testing Part2: Java EE | Main | Was ist JSF überhaupt? »

HOWTO test EJB3 with JUnit outside of the container

Since EJB 3.0, testing should be easy. And it IS much more simpler than with EJB 2.1, because you dont need to have an application server (AS) up and running

Chapter 12 in the book "Pro EJB 3: Java Persistence API" by Mike Keith and Merrick Schincariol was very useful for me.

I'm using Netbeans 6.0, EJB3, JPA and a MySQL-Datebase and i write integration tests. (as defined in the book above: focus on use-cases, decoupled from appserver, making full use of external resources such as database)

First of all i added the library JUnit (4.1) to my Netbeans-Project You can then rightclick on a sourcefile and select > Tools > Create JUnit Tests

Using the Entity Manager
If you're using the Entity Manager (EM) you must create one for your tests. Because no AS is around, you need an EM Factory and for that you need a Persistence Provider. Add the library TopLink Essentials to the project. Without the AS we cant use JTA datasource with JNDI, so i created a mysql testdatabase and added a persistence-unit for testpurposes. To use it i need the MySQL JDBC Driver-library in my project.
My persistence.xml looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
  <persistence-unit name="Project-ejbPU" transaction-type="JTA">
    <jta-data-source>jdbc/project_database_jndi_name</jta-data-source>
  </persistence-unit>
  <persistence-unit name="ProjectTest-ejbPU" transaction-type="RESOURCE_LOCAL">
    <class>project.model.entity1</class>
    <class>project.model.entity2</class>
    <class>project.model.entity3</class>
    <class>project.model.entity4</class>
    <class>project.model.entity5</class>
    <properties>
        <property name="toplink.ddl-generation" value="create-tables"/>
        <property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver" />
        <property name="toplink.jdbc.url" value="jdbc:mysql://localhost:3306/project_test_db_name" />
        <property name="toplink.jdbc.user" value="root"/>
        <property name="toplink.jdbc.password" value="xxx"/>
     </properties>
  </persistence-unit>
</persistence>

Note that the transaction-type should be RESOURCE_LOCAL and not JTA, because we dont want any trouble with JNDI-stuff ;) I've been told that every entity has to be defined explicitly in the persistence.xml

Now you need to create and use the EM in your tests
Change the setUp()-method in the testclass to the following:

    @Before
    public void setUp() {
        yourbean.em = Persistence
.createEntityManagerFactory("ProjectTest-ejbPU")
.createEntityManager(); // your own setUp-stuff }
Now you can write your tests as if your app is deployed in an AS.

About

This page contains a single entry from the blog posted on 20.03.08 15:32.

The previous post in this blog was Testing Part2: Java EE.

The next post in this blog is Was ist JSF überhaupt?.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.31