【问题标题】:JUnit and Spring WebflowJUnit 和 Spring Webflow
【发布时间】:2015-11-27 17:24:51
【问题描述】:

我在使用 JUnit 测试我的 Spring webflows 时遇到问题。

按照文档中的建议,我编写了一个扩展 AbstractXmlFlowExecutionTests 的测试类。

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");
    }

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

@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。异常打印路径并显示 FileNotFound 但路径确实存在(将此路径复制并粘贴到编辑器 - 文件 - 打开工作。

我在这里做错了什么?

非常感谢。

我正在使用 spring-webflow-2.3.2 和 jUnit 4

【问题讨论】:

    标签: java spring junit4 spring-webflow


    【解决方案1】:

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

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

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

    也许有人可以提出更好的解决方案?

    【讨论】:

    • 尽管已经很老了,但我仍然倾向于回答:最近我创建了一个库(现在依赖于 spring WebFlow 2.3.3-RELEASE)来简化单元测试 Web 流。您可以提供一个文件或 URL 资源,或者定义您自己的 ResourceMapper (Object -> FlowDefinitionResource) 来使用。我确信我错过了很多选择,但我仍然建议你试一试。见github.com/lhug/spring-webflow-testing可通过Jcenter获得
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-09
    • 2016-07-26
    相关资源
    最近更新 更多