[jboss-user] [JBoss Seam] - Re: Possible to write a rollback SeamTest?

RobJellinghaus do-not-reply at jboss.com
Fri Jul 14 00:40:33 EDT 2006


Hm.  No, actually, that's not the problem.  I mean, I was sticking that @Resource tag into a SeamTest instance.  Why should E-EJB inject into a SeamTest?  Nothing else does....

What I wanted was the equivalent of "Component.getInstance("sessionContext")" for the EJB session context, that I could use from within the SeamTest.  That would seem cleaner than your getTransactionManager() suggestion.  But lacking that, I did this instead:

@Name("testAction")
  | @Scope(ScopeType.CONVERSATION)
  | public class TestAction implements Serializable
  | {
  |    private static final Logger log = Logger.getLogger(TestAction.class);
  | 
  |    @In(create=true)
  |    private transient EntityManager entityManager;
  | 
  |    @Resource
  |    private SessionContext ctx;
  | 
  |    /**
  |     * Alter the passed-in BlogPost, then flush.  If failure, then setRollbackOnly and
  |     * rethrow.
  |     */
  |    public void alterBlogPost (BlogPost bp) {
  |       try {
  |          bp.setTitle("newTestTitle");
  | 
  |          entityManager.flush();
  |       } catch (RuntimeException e) {
  |          log.debug("Could not flush changed blog post", e);
  |          ctx.setRollbackOnly(); // this line turns out to be optional!!!
  |          throw e;
  |       }
  |    }
  | }

Then changed my test to:
            testAction = (TestAction)Component.getInstance("testAction", true);
  |             BlogPost prior1 = priorBlogPosts.get(0);
  | 
  |             try {
  |                testAction.alterBlogPost(prior1);
  | 
  |                // should throw a RuntimeException, so shouldn't get here
  |                assert false;
  |             } catch (RuntimeException e) {
  |                log.debug("Caught expected exception", e);
  |             }

Works great :-)  The sessionContext gets @Resource-injected into my TestAction, which flushes, blows up, and does setRollbackOnly; then it rethrows to my test case, which catches and ignores the exception.  

Result:  my test case passes just fine, and I avoid having to use your hacky workaround.  But thanks for mentioning it anyway, you never know when you might need something like that :-D

The WEIRD thing, though, is that even if you leave out the ctx.setRollbackOnly() line, the action still rolls back when the runtime exception gets thrown out of it!  Is this expected?  It surprised me, but actually it's kind of cool.  Is this standard Seam behavior or standard EJB3 behavior, or something else?

You never know what a test will teach you....
Cheers!
Rob

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3957961#3957961

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3957961



More information about the jboss-user mailing list