[jboss-user] [JBoss Seam] - Re: Unit Testing EL in EJB-QL/HQL
mrobinson28
do-not-reply at jboss.com
Fri Apr 13 15:30:10 EDT 2007
Ok, in my unit test I originally had:
| EntityManager entityManager = emf.createEntityManager();
|
I have replaced it with:
| EntityManager entityManager = new EntityManagerProxy( emf.createEntityManager() );
|
Now the exception is:
| java.lang.IllegalStateException: No application context active
| at org.jboss.seam.ScopeType.getContext(ScopeType.java:139)
| at org.jboss.seam.Component.getInstance(Component.java:1589)
| at org.jboss.seam.Component.getInstance(Component.java:1567)
| at org.jboss.seam.Component.getInstance(Component.java:1562)
| at org.jboss.seam.core.Expressions.instance(Expressions.java:219)
| at org.jboss.seam.persistence.QueryParser.<init>(QueryParser.java:60)
| at org.jboss.seam.persistence.QueryParser.<init>(QueryParser.java:33)
| at org.jboss.seam.persistence.EntityManagerProxy.createQuery(EntityManagerProxy.java:57)
|
The class/method that I am attempting to unit test looks like:
| @Stateful
| @Scope(ScopeType.SESSION)
| @Name("userBrowser")
| @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
| public class UserBrowserAction implements Serializable, UserBrowser {
|
| @Logger
| private Log log;
|
| @PersistenceContext
| private EntityManager entityManager;
|
| @DataModel
| private List<User> users;
|
| @DataModelSelection(value = "users")
| private User selectedUser;
|
| @Factory("users")
| @Observer("newUserRegistered")
| @SuppressWarnings("unchecked")
| public void getUsers() {
| log.debug("Invoke factory for users");
|
| this.users = this.entityManager.createQuery("from User user where user.username <> #{currentUser.username} ").getResultList();
|
| log.debug("Found #0 users", this.users.size());
| }
|
| @Destroy
| @Remove
| public void destroy() {
| }
| }
|
My unit test is:
| public class UserBrowserTest extends SeamTest {
|
| @Test (groups = "unit")
| @SuppressWarnings("unchecked")
| public void testGetUsers() throws Exception {
| EntityManagerFactory emf = Persistence.createEntityManagerFactory("test-persistence-unit");
| EntityManager entityManager = new EntityManagerProxy( emf.createEntityManager() );
|
| // create a log to inject
| Log log = Logging.getLog(UserBrowserAction.class);
|
| UserBrowser userBrowser = new UserBrowserAction();
|
| super.setField(userBrowser, "entityManager", entityManager);
| super.setField(userBrowser, "log", log);
|
| // invoke method
| userBrowser.getUsers();
|
| // inspect the property
| List<User> users = (List<User>) super.getField(userBrowser, "users");
|
| assert users != null;
| assert users.size() == 2;
| }
| }
|
So my questions:
| * From the exception it looks like I may have to provide more than I am currently via something in org.jboss.seam.mock.* e.g currentUser (In the application currentUser is a session scoped component that is instantiated when a user logs in). Are there any examples of using these mock objects anywhere?
| * Currently I am extending SeamTest which I understand is used for integration testing. Is it incorrect to do this when unit testing; several of the methods provided seem helpful in unit testing also. Would it be feasible to override init so that the embedded container is not started for stand alone unit test (or maybe have something like SeamUnitTest?
| * In general I seem to be having difficulties writing unit test because it occurs over and over again that some method is accessing a variable that is within scope during normal program executions e.g. in the code above currentUser is within scope but is not injected into the component that is accessing it. Is this bad practice? I tested re-writing the class that I am trying to test to contain and instance of the currentUser object injected using @In and then accessing that object from the method that performs the query and this works, both in the application and in my unit test if I manually inject that object.
| * Is it possible to manually inject objects into different scopes programmatically for unit testing?
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4037218#4037218
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4037218
More information about the jboss-user
mailing list