【问题标题】:Testing Spring Web Flow exception event测试 Spring Web Flow 异常事件
【发布时间】:2014-04-28 16:16:02
【问题描述】:

我的流 XML (my-flow.xml) 中有以下设置:

<view-state id="exceptionViewState" view="general_error" model="oneObjet"/>
...
<view-state id="previousViewState" view="previous_view" model="oneObjet">
    <transition on="actionConfirmed" to="endState" />
</view-state>
...
<global-transitions>
    <transition on-exception="com.xxx.exception.GeneralException" to="exceptionViewState"/>
</global-transitions>

我正在尝试通过扩展 AbstractXmlFlowExecutionTests 来测试这个流 XML

public void testMyFlow_Exception() {
    setCurrentState("previousViewState");

    MockExternalContext context = new MockExternalContext();
    context.setEventId("com.xxx.exception.GeneralException");
    resumeFlow(context);

    assertCurrentStateEquals("exceptionViewState");
    assertResponseWrittenEquals("general_error", context);
}

当我运行此测试时,我收到以下错误:

org.springframework.webflow.engine.NoMatchingTransitionException: No transition found on occurence of event 'com.xxx.exception.GeneralException' in state 'previousViewState' of flow 'my-flow' -- valid transitional criteria are array<TransitionCriteria>[actionConfirmed] -- likely programmer error, check the set of TransitionCriteria for this state

乍一看,似乎是使用MockExternalContext#setEventId() 方法(我认为不适用于异常)尝试模拟抛出的异常引起的问题。但我想不出另一种方法来做到这一点。关于如何实现这一点的任何想法?

【问题讨论】:

  • 你需要有一个过渡:
  • 我已经有一个过渡,但它是一个“异常”的过渡。测试目的是模拟异常转换,而不是正常转换。
  • 全局转换应该在视图状态之下和结束状态之上。重新排列并尝试。
  • global-transitions 在我的实际代码中确实低于视图状态。我修改了帖子以反映这一点。但是,最终状态不允许低于全局转换(XML 模式验证错误)。澄清一下,这个流程运行良好;这是我无法开始工作的异常转换的单元测试

标签: java unit-testing exception spring-webflow


【解决方案1】:

我找到了解决这个问题的方法,方法是对 Web 流中调用的服务进行存根以抛出异常 GeneralException。这让我可以测试异常流程:

public class ExceptionFlowTest extends AbstractXmlFlowExecutionTests {

    private MyService myService;

    protected void setUp() {
        myService = new MyService() {

            public ObjectDTO getObject() throws GeneralException {
                throw new GeneralException();
            }

        };
    }

    @Override
    protected FlowDefinitionResource getResource(FlowDefinitionResourceFactory resourceFactory) {
        return resourceFactory.createFileResource("WebContent/WEB-INF/flows/my-flow.xml");
    }

    @Override
    protected void configureFlowBuilderContext(MockFlowBuilderContext builderContext) {
        builderContext.registerBean("myService", myService);
    }

    public void testMyFlow_Exception() {

        MutableAttributeMap input = new LocalAttributeMap();
        MockExternalContext context = new MockExternalContext();

        startFlow(input, context);

        assertCurrentStateEquals("exceptionViewState");
    }

}

我不必更改我的流 XML:

<flow xmlns="http://www.springframework.org/schema/webflow"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/webflow
                          http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd"
      start-state="getObject">

    <persistence-context />  

    <var name="object" class="com.xxx.model.ObjectDTO" />

     <action-state id="getObject">
        <evaluate expression="myService.getObject()" result="object" />
        <transition to="nextView" />
     </action-state>
...
    <global-transitions>
        <transition on-exception="com.xxx.exception.GeneralException" to="exceptionViewState"/>
    </global-transitions>

</flow> 

【讨论】:

    猜你喜欢
    • 2012-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-09
    • 1970-01-01
    • 2013-10-10
    • 1970-01-01
    • 2012-03-20
    相关资源
    最近更新 更多