22. 测试流程
本章向您展示如何测试流程。
22.1. 扩展AbstractXmlFlowExecutionTests
要测试基于 XML 的流定义的执行,您必须扩展 AbstractXmlFlowExecutionTests,如下所示:
public class BookingFlowExecutionTests extends AbstractXmlFlowExecutionTests {
}
22.2. 指定要测试的流的路径
至少,您必须重写 getResource(FlowDefinitionResourceFactory) 以返回您希望测试的流的路径,如下所示:
@Override
protected FlowDefinitionResource getResource(FlowDefinitionResourceFactory resourceFactory) {
return resourceFactory.createFileResource("src/main/webapp/WEB-INF/hotels/booking/booking.xml");
}
22.3. 注册流依赖项
如果您的流程依赖于外部管理的服务,您还必须重写 configureFlowBuilderContext(MockFlowBuilderContext) 以注册这些服务的存根或模拟对象,如下所示:
@Override
protected void configureFlowBuilderContext(MockFlowBuilderContext builderContext) {
builderContext.registerBean("bookingService", new StubBookingService());
}
如果您的流程扩展自另一个流程,或者具有扩展其他状态的状态,则还必须重写 getModelResources(FlowDefinitionResourceFactory) 以返回父流程的路径,如下所示:
@Override
protected FlowDefinitionResource[] getModelResources(FlowDefinitionResourceFactory resourceFactory) {
return new FlowDefinitionResource[] {
resourceFactory.createFileResource("src/main/webapp/WEB-INF/common/common.xml")
};
}
22.4. 测试流程启动
以下示例展示了如何让你的首次测试练习流的启动:
public void testStartBookingFlow() {
Booking booking = createTestBooking();
MutableAttributeMap input = new LocalAttributeMap();
input.put("hotelId", "1");
MockExternalContext context = new MockExternalContext();
context.setCurrentUser("keith");
startFlow(input, context);
assertCurrentStateEquals("enterBookingDetails");
assertTrue(getRequiredFlowAttribute("booking") instanceof Booking);
}
Assertions generally verify that the flow is in the state you expect.
22.5. 测试流事件处理
您可以定义额外的测试来执行流事件处理行为。 您的目标应该是遍历流的所有路径。 您可以使用便捷的setCurrentState(String)方法跳转到您希望开始测试的流状态,如下所示:
public void testEnterBookingDetails_Proceed() {
setCurrentState("enterBookingDetails");
getFlowScope().put("booking", createTestBooking());
MockExternalContext context = new MockExternalContext();
context.setEventId("proceed");
resumeFlow(context);
assertCurrentStateEquals("reviewBooking");
}
22.6. 模拟子流程
为了测试调用子流程,您可以注册一个子流程的模拟实现,该实现会断言输入是否正确传递,并为您的测试场景返回正确的结果,如下所示:
public void testBookHotel() {
setCurrentState("reviewHotel");
Hotel hotel = new Hotel();
hotel.setId(1L);
hotel.setName("Jameson Inn");
getFlowScope().put("hotel", hotel);
getFlowDefinitionRegistry().registerFlowDefinition(createMockBookingSubflow());
MockExternalContext context = new MockExternalContext();
context.setEventId("book");
resumeFlow(context);
// verify flow ends on 'bookingConfirmed'
assertFlowExecutionEnded();
assertFlowExecutionOutcomeEquals("finish");
}
public Flow createMockBookingSubflow() {
Flow mockBookingFlow = new Flow("booking");
mockBookingFlow.setInputMapper(new Mapper() {
public MappingResults map(Object source, Object target) {
// assert that 1L was passed in as input
assertEquals(1L, ((AttributeMap) source).get("hotelId"));
return null;
}
});
// immediately return the bookingConfirmed outcome so the caller can respond
new EndState(mockBookingFlow, "bookingConfirmed");
return mockBookingFlow;
}