NetBeans Forums
| View previous topic :: View next topic |
| Author |
Message |
walec51
Joined: 17 Mar 2010 Posts: 9
|
Posted: Sat Mar 20, 2010 9:35 pm Post subject: JUnit tests with Glassfish EJB's |
|
|
Hello,
Something strange is happening in my glassfish unit tests. It seams that the embed container persistence doesn't work.
I have a test like this:
| Code: |
public class PlayerDaoTest {
static EJBContainer ejbCon;
private PlayerDao dao;
@Before
public void initEjb() throws Exception {
ejbCon = EJBContainer.createEJBContainer();
Context ctx = ejbCon.getContext();
dao = (PlayerDao) ctx.lookup("java:global/classes/PlayerDao");
}
@Test
public void login() {
Player pr = dao.registerPlayer("someone@some.pl", "lolo");
Player pl = dao.login("someone@some.pl", "lolo");
assertEquals(pr.getEmail(), pl.getEmail());
assertEquals(pr.getId(), pl.getId());
}
}
|
This works well in my web client but it seams that in the tests the player added in the register method is forgotten when the execution proceeds to the login method. I get a NoResultException there which is expected when a Player with the corresponding email and password is not found.
Here's the rest of my code:
| Code: |
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
public class PlayerDao {
@PersistenceContext(unitName="prodPU")
private EntityManager em;
/**
*
* @param email
* @param password
* @return registered player
* @throws EntityExistsException - when a user with the passed email
* already exists
*/
public Player registerPlayer(String email, String password)
throws EntityExistsException {
Query q = em.createNamedQuery("findPlayerByEmail");
q.setParameter("email", email);
List r = q.getResultList();
if(r.size() != 0) {
throw new EntityExistsException(conf.getGlobalResourceBundle()
.getString("register.mail.exists.error"));
}
Player p = new Player();
p.setEmail(email);
p.setPasswordHash(calcuatePasswordHash(password));
p.setRegisterDate(Calendar.getInstance().getTime());
p.setGoldCoins(Integer.valueOf(
conf.getGlobalProperty("register.gold")));
em.persist(p);
return p;
}
/**
*
* @param email
* @param password
* @return logged in player
* @throws NoResultException - when email and password don't match
*/
public Player login(String email, String password)
throws NoResultException {
Query q = em.createNamedQuery("findPlayerByEmailAndPasswordHash");
q.setParameter("email", email);
q.setParameter("passwordHash", calcuatePasswordHash(password));
Player p = (Player)q.getSingleResult();
p.setLastSeenDate(Calendar.getInstance().getTime());
p.setOnline(true);
em.merge(p);
return p;
}
}
|
| Code: |
<?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="prodPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/__default</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
</properties>
</persistence-unit>
</persistence>
|
Please give me a clue what em I doing wrong ?
Or does this look like a bug ? |
|
| Back to top |
|
 |
walec51
Joined: 17 Mar 2010 Posts: 9
|
Posted: Tue Mar 23, 2010 11:07 am Post subject: |
|
|
I've done a simple test project to inspect this and the results a rather interesting.
| Code: |
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
public class AnBean {
@PersistenceContext(unitName="testPU")
private EntityManager em;
public void addAnEntity(String someData) {
AnEntity e = new AnEntity();
e.setSomeData(someData);
em.persist(e);
}
public AnEntity getAnEntity(String someData) {
Query q = em.createNamedQuery("AnQuery");
q.setParameter("data", someData);
return (AnEntity) q.getSingleResult();
}
}
|
| Code: |
@Entity
@NamedQuery(name="AnQuery", query="select e from AnEntity e where e.someData = :data")
public class AnEntity implements Serializable {
@Id
@GeneratedValue
private Long id;
private String someData;
public String getSomeData() {
return someData;
}
public void setSomeData(String someData) {
this.someData = someData;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
|
| Code: |
public class AnBeanTest {
static EJBContainer ejbCon;
private AnBean bean;
@Before
public void initEjb() throws Exception {
ejbCon = EJBContainer.createEJBContainer();
Context ctx = ejbCon.getContext();
bean = (AnBean) ctx.lookup("java:global/classes/AnBean");
}
@Test
public void beanTestA() { //this one is successfull
bean.addAnEntity("xxx");
bean.getAnEntity("xxx");
}
@Test
public void beanTestB() { //this one faild
bean.addAnEntity("xxx");
bean.getAnEntity("xxx");
}
@After
public void closeEjb() {
bean = null;
ejbCon.close();
}
}
|
Test A is successful and the exactly same test B fails.
Please tell me is there something wrong in my After and Before methods ?
Or is this a bug ?[/code] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
|