我有一个父工作流程(ParentWorkflow)调用子工作流程(ChildWorkflow),我试图测试该呼叫。
父代码如下所示:
ido-ignore-directories
我想嘲笑
public class ParentWorkflow {
private final ChildWorkflowClientFactory childWorkflowClientFactory =
new ChildWorkflowClientFactoryImpl();
public void runWorkflow() {
new TryCatch() {
@Override
protected void doTry() throws Throwable {
Promise<Void> workflowFinished = childWorkflowClient.childWorkflow(x);
...
}
...
}
}
打电话,但是当我连接单元测试时,我似乎没有选择注入客户端工厂,单元测试代码如下所示:
childWorkflowClient.childWorkflow(x)
我似乎无法将任何内容传递到工作流实现类中,还有其他方法可以模拟子工作流吗?
答案 0 :(得分:5)
您可以直接模拟其依赖项来测试工作流代码,而无需使用 workflowTest :
/**
* Rule is still needed to initialize asynchronous framework.
*/
@Rule
public WorkflowTest workflowTest = new WorkflowTest();
@Mock
private ActivitiesClient mockActivities;
@Mock
private BWorkflowClientFactory workflowFactory;
@Before
public void setUp() throws Exception {
// set up mocks
initMocks(this);
}
@Test
public void myTest() {
AWorkflowImpl w = new AWorkflowImpl(workflowFactory);
w.execute(); // whatever execute method of the workflow
}
此方法允许测试封装在其他对象中的部分工作流,而不是整个工作流。
如果由于某种原因(例如,您使用的是其他测试框架而不是JUnit),您不想依赖于WorkflowTest @Rule异步代码可以始终使用AsyncScope执行:
@Test
public void asyncTest() {
AsyncScope scope = new AsyncScope() {
protected void doAsync() {
// Any asynchronous code
AWorkflowImpl w = new AWorkflowImpl(workflowFactory);
w.execute(); // whatever execute method of the workflow
}
};
scope.eventLoop();
}
答案 1 :(得分:0)
编辑:以下仅适用于SpringWorkflowTest;由于某种原因,WorkflowTest没有addWorkflowImplementation
。
使用WorkflowTest的正确方法是为子工作流添加模拟实现,而不是添加实际类型:
@Rule
public SpringWorkflowTest workflowTest = new SpringWorkflowTest();
@Mock
private Activities mockActivities;
@Mock
private ChildWorkflow childWorkflowMock;
private ParentWorkflowClientFactory workflowFactory
= new ParentWorkflowClientFactoryImpl();
@Before
public void setUp() throws Exception {
// set up mocks
initMocks(this);
workflowTest.addActivitiesImplementation(mockActivities);
workflowTest.addWorkflowImplementationType(ParentWorkflowImpl.class);
workflowTest.addWorkflowImplementation(childWorkflowMock);
...
}
当您使用工厂时,框架将调用此mock而不是实际实现。