JUnit and Spring Webflow



我在用JUnit测试Spring网络流时遇到问题。

正如文档中所建议的那样,我编写了一个扩展AbstractXmlFlowExecutionTest的测试类。

public class MyFlowTest extends AbstractXmlFlowExecutionTests {
    MockExternalContext context = new MockExternalContext();
    String workingDirectory = null;
    @Override
    protected FlowDefinitionResource        getResource(FlowDefinitionResourceFactory resourceFactory) {
        return resourceFactory.createFileResource("WebContent/flows/myflow-flow.xml");
    }

测试下的webflow继承自一个抽象的父webflow,该父webflow定义了一些全局转换。所以我尝试覆盖getModelResources来提供这个父网络流。但这根本不起作用。

@Override
protected FlowDefinitionResource[] getModelResources(FlowDefinitionResourceFactory resourceFactory) {
    FlowDefinitionResource[] flowDefResources = new FlowDefinitionResource[1];
    see below...
    return flowDefResources;
}

我的具体问题是:

当我将resourceFactory.createFileResource用于父流时,名称不正确,因此我得到一个异常消息Unable to find flow 'main/parentflow' to inherit from

当我使用resourceFactory.createResource(String path, null, "main/parentflow"时,定义流的.xml永远找不到。Exception打印路径并显示FileNotFound,但路径确实存在(将此路径复制并粘贴到编辑器-文件-打开有效。

我在这里做错了什么?

非常感谢。

我使用的是spring-webflow-2.3.2和jUnit 4

所以我通过查看AbstractXmlFlowExecutionTests的来源发现了问题:FlowDefinitionResourceFactory.createFileResource的工作方式与resourceFactory.createResource(String path, AttributeMap attributes, String flowID)不同。第二种是使用类加载器而不是简单的文件加载器。因此,解决方案是在类路径中提供XML文件,以便可以找到它。类加载器似乎不允许只打开本地计算机上的任何文件。

我想我会在我的maven构建中添加一个步骤,将所有流定义复制到target/WEB_INF目录中,仅用于测试目的。

对我来说,这似乎很丑陋,我不明白createResource为什么要使用这种方法。

也许有人能提出一个更好的解决方案?

最新更新