MockTaskRequest

mlrains 
Joined: 03/13/2008
User offline. Last seen 2 weeks 4 days ago.

Please provide an example of using MockTaskRequest for unit testing process flows.

mark  Appistry employee
Joined: 12/21/2007
User offline. Last seen 22 weeks 2 days ago.

mlrains,

I need to confirm what you are asking so that I answer you correctly. Is your goal to test your process flows without a normal taskRequest?

I am not sure if you are going to get the desired result if that is the question. The MockTaskRequest allows you to use a fake request service in your task that would normally be sent by the process flow to your task. Is that what you are interested in?

Thanks,
Mark

guerry  Appistry employee
guerry's picture
Joined: 12/21/2007
User offline. Last seen 2 hours 59 sec ago.

Hey,

Actually, the MockTaskRequest is primarily used for unit testing tasks, since their interface requires a task request object.

We're aware that process flow testing is painful. This is something that we intend to address, but I don't have a schedule to share yet for that.

mlrains 
Joined: 03/13/2008
User offline. Last seen 2 weeks 4 days ago.

I am interested in using the MockTaskRequest for unit testing tasks and their respective request objects. This is what I am interested in...

guerry  Appistry employee
guerry's picture
Joined: 12/21/2007
User offline. Last seen 2 hours 59 sec ago.

Part of the answer will depend on the approach you are taking with your tasks. I'm not sure if my examples below will address a particular scenario you are shooting for.

Taking what I know so far, I'll jump right in.

If you Java task has to have knowledge of the task request to do its work, it could hold a public ITaskRequest object like this:

import com.appistry.task.ITaskRequest;
import com.appistry.task.Annotations.TaskRequest;
import com.appistry.task.Annotations.TaskParameter;
 
public class SimpleJavaTask {
   @TaskRequest
   public ITaskRequest request;
 
   public void doLowercase() {
      String foo = (String)request.get("myString" );
      request.put("myString", foo.toLowerCase());
   }
}

Upon execution, CloudIQ Engine injects the transaction's request object into this instance variable "request", and the task code takes advantage of the request like it would any other instance variable. So a simple junit test for my SimpleJavaTask above would be:

import junit.framework.TestCase;
import com.appistry.task.MockTaskRequest;
 
public class SimpleJavaTaskTest extends TestCase {
   public void testDoLowercase() {
      MockTaskRequest request = new MockTaskRequest();
      request.put("myString", "LIONS, TIGERS, AND BEARS! OH MY!");
 
      SimpleJavaTask task = new SimpleJavaTask();
      task.request = request;
 
      task.doLowercase();
 
      assertEquals("lions, tigers, and bears! oh my!", 
                   request.get("myString"));
   }
}

Now this test works because my task code actually uses the request object directly. However, if I take fuller advantage of CloudIQ's annotation-based injection at runtime, I can write tasks that do not have direct knowledge of the task request or CloudIQ at all, and yet can run in Engine. Each of the annotated values below are injected with Task Request attributes at runtime in CloudIQ Engine, and yet I can test the method in a unit test without using MockTaskRequest.

import com.appistry.task.Annotations.TaskParameter;
import com.appistry.task.Annotations.TaskReturnValue;
 
public class AppendStringsTask {
   @TaskReturnValue(name="newString")
   public String append(@TaskParameter("first") String first,
                        @TaskParameter("second") String second) {
      return new StringBuffer(first).append(second).toString();
   }
}

import junit.framework.TestCase;
 
public class AppendStringsTaskTest extends TestCase {
   public void testAppendStringsTask() {
      AppendStringsTask task = new AppendStringsTask();
 
      assertEquals("foobar", task.append("foo", "bar"));
   }
}

I'm not sure this has been helpful at this point. Is there a particular task / request scenario you are trying to test?