当前位置: 动力学知识库 > 问答 > 编程问答 >

persistence - Testing EJB 3.1 with JUnit gets error Invalid resource

问题描述:

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!

网友答案:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: