I’m trying to test my Session Beans with JUnit, but I can’t. I’ve tried a lot of method, but still get some exceptions.
Here is what I need:
- I have a few Stateless Session Beans I need to test. Each has the same
@PersistenceContextand uses anEntityManager - With my test cases I need to test their methods. For instance: if I add an user with username X and then I try to add another one with the same username, I want to catch an Exception.
Can someone provide a simple and short generic test example? I’ve already read many, but I always get an error (I get NullPointerException for the EntityManager when I call a method like: sessionBean.method() (which does, for instance, entityManager.find(...)), or I am not able to initialize the Context, or other PersistenceException).
I solved creating a
Stateless Session Beanand injecting itsEntity Managerto test classes. I post the code in case someone will need it:Then I can use
proxy.find()to get the entities I need, oproxy.getEntityList()to execute a query to retrieve all the instance of an Entity. Or I can add other methods if I want.