我正在尝试为AWS SWF工作流编写单元测试。下面是我想测试
的代码@Override
public void execute(String abc) {
new TryCatch() {
@Override
protected void doTry() throws Throwable {
Promise<SomeObject> temp = activityClient.action(abc);
again(temp, abc);
}
@Override
protected void doCatch(Throwable e) throws Throwable {
throw new RuntimeException(e);
}
};
}
@Asynchronous
public void again(Promise<SomeObject> someObject, String abc) {
// Do Something
}
我的Test类如下:
public class SomeWorkflowTest extends AbstractTestCase {
@Rule
public WorkflowTest workflowTest = new WorkflowTest();
List<String> trace;
private SomeWorkflowClientFactory workflowFactory = new SomeWorkflowClientFactoryImpl();
@Before
public void setUp() throws Exception {
trace = new ArrayList<String>();
// Register activity implementation to be used during test run
SomeActivitiesImpl activitiesImpl = new SomeActivitiesImpl() {
@Override
public SomeObject performHostRecovery(String abc) {
trace.add("ABC: " + abc);
SomeObject testObject = new SomeObject();
return testObject;
}
};
workflowTest.addActivitiesImplementation(activitiesImpl);
workflowTest.addWorkflowImplementationType(SomeWorkflowImpl.class);
}
@Test
public void testWorkflowExecutionCall() throws Throwable {
SomeWorkflowClient workflow = workflowFactory.getClient("XZY");
workflow.execute("XYZ");
List<String> expected = new ArrayList<String>();
expected.add("ABC: abc");
AsyncAssert.assertEqualsWaitFor("Wrong Wrong", expected, trace, null);
}
}
我已经使用SWF测试文档来编写上面的测试类。然而,我正在测试的方法(execute())正在调用同一类中的另一个方法。我不关心内部方法的执行,并希望模拟它,但给定工作流类对象实例化的方式,我不清楚如何模拟内部方法。
有人能指出这一点吗?谢谢
您实际上可以实例化一个工作流对象或workflow在测试方法中使用的任何其他对象:
@Test
public void testWorkflowExecutionCall() throws Throwable {
SomeWorkflow workflow = new SimpleWorkflow(...);
workflow.execute("XYZ");
List<String> expected = new ArrayList<String>();
expected.add("ABC: abc");
AsyncAssert.assertEqualsWaitFor("Wrong Wrong", expected, trace, null);
}
它工作是因为WorkflowTest在一个虚拟测试工作流的上下文中执行测试方法。代码
SomeWorkflowClient workflow = workflowFactory.getClient("XZY");
workflow.execute("XYZ");
实际上在这个虚拟工作流的上下文中创建了一个子工作流。但是没有什么可以阻止你在不创建子工作流的情况下直接执行任何异步代码。