Sunteți pe pagina 1din 6

My EJB Module JUnit Testing Procedure

1. Create a new JavaEE EJB Module Project


2. Add your Entity and Entity Manager or DAO java files in the Source Packages directory.
3. After Step 2. You should have a persistence.xml file in the Configuration Files directory.
4. Set up your persistence.xml file like the example below, so you do not have to rely on
Glassfish and JNDI stuff,

persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>


<persistence version="2.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_2_0.xsd">

<persistence-unit name="FooEJBPU" transaction-type="RESOURCE_LOCAL">

<jta-data-source>jdbc/alertmail</jta-data-source>

<class>FooMail.Users</class>

<exclude-unlisted-classes>true</exclude-unlisted-classes>

<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://localhost:3306/alertmail"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="root"/>
</properties>
</persistence-unit>

</persistence>

Above the FooMail is the directory in the Source Packages directory and Users is the
Entity file
Generated by NetBeans from the first alertmail database we were originally given. I
chose to test the Users Entity through the UsersEntityManagerBean.
5. Next you will need to add the MYSQL library to your Libraries directory so the
MYSQL driver will be available during the test.

6. Know you will need to create a Test file in the Test Packages directory.

7. The example below is the Test file FooTest I created to test persistence through EJB.
When I created the Test file I also created the directory FooTest under the Test Packages
directory where it would reside,

FooTest.java:

package FooTest;

import FooMail.UsersEntityManagerBean;
import FooMail.Users;
import javax.persistence.Persistence;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import junit.framework.TestCase;
import static org.junit.Assert.*;

public class FooTest extends TestCase {

//IMORTANT NOTE: To Test The Admin You Must First Add Her In The DB
//Admin To Check By Persistence If In Data Base AlertMail
private static final String ADMIN_EMAIL = "1@uco.edu";
private static final String ADMIN_PASSWORD = "root";
private static final short ADMIN_SYSTEM_ROLE = 1;

//New User To Persist To Data Base AlertMail


private static final String USER_EMAIL = "2@uco.edu";
private static final String USER_PASSWORD = "user";
private static final short USER_SYSTEM_ROLE = 0;
private EntityManagerFactory emf;
private EntityManager em;

public void setUp() {

System.out.println("Creating Entity Manager Factory");


emf = Persistence.createEntityManagerFactory("FooEJBPU");
if(emf.equals(null))
System.out.println("NULL EMF");
em = emf.createEntityManager();
System.out.println("Creating Entity Manager");
createTestData();
}
public void tearDown() {
System.out.println("Tear Down");
if (em != null) {
removeTestData();
em.close();
System.out.println("Closing Entity Manager");
}
if (emf != null) {
emf.close();
}
}

private void createTestData() {


System.out.println("Creating new user");
Users user = new Users();
user.setEmail(USER_EMAIL);
user.setPassword(USER_PASSWORD);
user.setSystemRole(USER_SYSTEM_ROLE);
em.getTransaction().begin();
System.out.println("Persisting to DB via Entity Manager");
em.persist(user);
em.getTransaction().commit();
}

private void removeTestData() {


System.out.println("Removing new user previously created");
em.getTransaction().begin();
Users user = em.find(Users.class, USER_EMAIL);
if (user != null) {
em.remove(user);
}
em.getTransaction().commit();
}

public void testInsertedUserIsAValidUser() throws Exception {


UsersEntityManagerBean uemb = new UsersEntityManagerBean();
uemb.em = em;
Users user = uemb.getUser(USER_EMAIL);
System.out.println("Using UsersEntityManagerBean to query new user previously
entered into DB");
if(!user.equals(null))
System.out.println("User was retrieved from the DB via
UsersEntityManagerBean");
assertNotNull(user);
assertEquals(USER_EMAIL, user.getEmail());
assertEquals(USER_PASSWORD, user.getPassword());
assertEquals(USER_SYSTEM_ROLE, (short)user.getSystemRole());
}

//IMORTANT NOTE: To Test The Admin You Must First Add Her In The DB
public void testAdminIsAValidUser() throws Exception {
UsersEntityManagerBean uemb = new UsersEntityManagerBean();
uemb.em = em;
Users user = uemb.getUser(ADMIN_EMAIL);
System.out.println("Using UsersEntityManagerBean to query admin in DB");
if(!user.equals(null))
System.out.println("Admin was retrieved from the DB via
UsersEntityManagerBean");
assertNotNull(user);
assertEquals(ADMIN_EMAIL, user.getEmail());
assertEquals(ADMIN_PASSWORD, user.getPassword());
assertEquals(ADMIN_SYSTEM_ROLE, (short)user.getSystemRole());
}
}

8. Cross your fingers and select Test by right click on project name and selecting Test.

My final test looked like this:

Creating Entity Manager Factory


Mar 8, 2011 11:13:35 PM org.hibernate.validator.util.Version <clinit>
INFO: Hibernate Validator bean-validator-3.0-JBoss-4.0.2
Mar 8, 2011 11:13:35 PM
org.hibernate.validator.engine.resolver.DefaultTraversableResolver detectJPA
INFO: Instantiated an instance of
org.hibernate.validator.engine.resolver.JPATraversableResolver.
[EL Warning]: 2011-03-08 23:13:37.918--ServerSession(24745276)--PersistenceUnitInfo
FooEJBPU has transactionType RESOURCE_LOCAL and therefore jtaDataSource will
be ignored
[EL Info]: 2011-03-08 23:13:37.934--ServerSession(24745276)--EclipseLink, version:
Eclipse Persistence Services - 2.0.1.v20100213-r6600
[EL Info]: 2011-03-08 23:13:39.166--ServerSession(24745276)--
file:/C:/Users/SE/Documents/NetBeansProjects/FooEJB/build/classes/_FooEJBPU login
successful
Creating Entity Manager
Creating new user
Persisting to DB via Entity Manager
Using UsersEntityManagerBean to query new user previously entered into DB
User was retrieved from the DB via UsersEntityManagerBean
Tear Down
Removing new user previously created
Closing Entity Manager
[EL Info]: 2011-03-08 23:13:39.743--ServerSession(24745276)--
file:/C:/Users/SE/Documents/NetBeansProjects/FooEJB/build/classes/_FooEJBPU
logout successful
Creating Entity Manager Factory
[EL Warning]: 2011-03-08 23:13:39.79--ServerSession(21411547)--PersistenceUnitInfo
FooEJBPU has transactionType RESOURCE_LOCAL and therefore jtaDataSource will
be ignored
[EL Info]: 2011-03-08 23:13:39.79--ServerSession(21411547)--EclipseLink, version:
Eclipse Persistence Services - 2.0.1.v20100213-r6600
Mar 8, 2011 11:13:39 PM
org.hibernate.validator.engine.resolver.DefaultTraversableResolver detectJPA
INFO: Instantiated an instance of
org.hibernate.validator.engine.resolver.JPATraversableResolver.
[EL Info]: 2011-03-08 23:13:39.899--ServerSession(21411547)--
file:/C:/Users/SE/Documents/NetBeansProjects/FooEJB/build/classes/_FooEJBPU login
successful
Creating Entity Manager
Creating new user
Persisting to DB via Entity Manager
Using UsersEntityManagerBean to query admin in DB
Admin was retrieved from the DB via UsersEntityManagerBean
Tear Down
Removing new user previously created
Closing Entity Manager
[EL Info]: 2011-03-08 23:13:39.977--ServerSession(21411547)--
file:/C:/Users/SE/Documents/NetBeansProjects/FooEJB/build/classes/_FooEJBPU
logout successful

S-ar putea să vă placă și