cancel
Showing results for 
Search instead for 
Did you mean: 

Unit Testing a Transition Listener

marcthomas2012
Champ in-the-making
Champ in-the-making
Hi,

I am currently building a workflow engine into our system using Activiti (which is great by the way). We are relying heavily on the use of listeners for triggering behaviour like adding data to our database etc.

I have provided a cut down version of the listener below to show roughly what I'm doing. Basically, the workflow configured in Activiti would add this listener as a Java class and I have added the two expressions to that configured listener.

What I would like to achieve in the unit test is to be able to pass in valid and invalid data to the notify method and verify the outcome.

My problem is though, I don't really know what I need to construct for the parameter in order to get the Activiti side of things to work and allow my code to extract the expression values.

Can anyone help?

Thanks
Marc


public class AddCommentTransitionListener implements ExecutionListener, Serializable {
   private static final Logger log = Logger.getLogger(AddCommentTransitionListener.class);

   private static final long serialVersionUID = 1L;

   private Expression manualCommentExpression;
   private Expression manualCommentTypeExpression;

   private String manualComment = null;
   private CommentType manualCommentType;

   public void notify(DelegateExecution execution) throws Exception {
      processConfiguration(execution);
                // Add the comment details to the database
   }

   private void processConfiguration(DelegateExecution execution) {
      if (execution != null) {
         extractManualCommentConfiguration(execution);
      }
   }

   private void extractManualCommentConfiguration(DelegateExecution execution) {
      if (manualCommentExpression != null) {
         manualComment = (String) manualCommentExpression.getValue(execution);

         if (manualCommentTypeExpression != null) {
            // TODO throw exception if we cannot determine the comment type or if the comment type is not
            // present
            manualCommentType = CommentType.lookup(Integer.parseInt((String) manualCommentTypeExpression
                  .getValue(execution)));
         } else {
            // TODO throw exception
         }
      }
   }
}

1 REPLY 1

marcthomas2012
Champ in-the-making
Champ in-the-making
Hi,

It seems that a little more digging and I have found the answer, so thought I'd share with anyone else.

The method I was using to get the values from the expressions was manualCommentExpression.getValue(execution)However, what I should have called was manualCommentExpression.getExpressionText() to get the text that has been configured.

That left needing to set the Expression objects in the listener, in my unit test which I did by changing the access modifiers to protected so that they can be accessed via the unit test. The in the unit test I did the following:

listener.manualCommentExpression = new FixedValue("This is my comment");
Marc