Reputation: 61
I am trying to test an Stateless bean with JUnit in netbeans. This bean uses an EntityManager.
@Stateless
public class myEjb{
@PersistenceContext
private EntityManager em;
public MyResult getResult(){
return em.find(...);
}
}
Then I write a test class.
public class myTest{
private static EJBContainer ec;
private static Context ctx;
@BeforeClass
public static void setUpClass(){
ec = EJBContainer.createEJBContainer();
ctx = ec.getContext();
}
....
}
When I run the test, it does not work. I obtain the following message: Invalid resource : mydb__pm The error occurs when this line is executed:
ec = EJBContainer.createEJBContainer();
If a change my bean by removing the entity manager, it works. So, it seems that I have a problem with the entity manager. My persistence.xml file is simple:
<persistence version="2.0" ...>
<persistence-unit name="MetisDemoPU" transaction-type="JTA">
<jta-data-source>MyDb</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties/>
</persistence-unit>
</persistence>
Finally, I create a JSF managed bean that called my EJB (which uses the entity manager) and it works.
@ManagedBean
@RequestScoped
public class myManagedBean{
@EJB
private OfferEjb offerEjb;
...
}
Any help would be appreciated!
Upvotes: 0
Views: 1590
Reputation: 61
Ok, I find a solution to my problem. I am now able to use JUnit to test my session bean with a persistence context. I am not a specialist, so my explanation will probably not be complete. With netbeans 7.2, there is an embedded glassfish server which is used for the test. It is necessary to configure the jdbc parameters in the domain.xml file and then for me it works. On my computer, this file is under
C:\Program Files\glassfish-3.1.2.2\glassfish\domains\domain\
I just add a jdbc connection pool and a jdvc jndi.
This article contains more details.
Upvotes: 1