我正在尝试为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
}
我的测试课程如下:
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 Testing Docs编写了上面的测试类。但是我正在测试的方法(execute())是在同一个类中调用另一个方法。我并不关心内部方法的执行,并且想要模拟它,但是考虑到实例化工作流类对象的方式,我不清楚如何模拟内部方法。
有人可以指出这个吗?
谢谢
答案 0 :(得分:0)
实际上,您可以实例化工作流对象或工作流在测试方法中使用的任何其他对象:
@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");
实际上在此虚拟工作流的上下文中创建子工作流。但是没有什么能阻止您在不创建子工作流的情况下直接执行任何异步代码。