简体   繁体   中英

In Spring Webflow unit test, how do you assert that a view state has a view of a given name?

I'm developing a Spring webflow, trying to use TDD so I've extended AbstractXmlFlowExecutionTests. I can't see an obvious way to assert what I would have thought would be a simple thing: that a view state has an associated view of a given name. For example, given this flow (excerpt):

<?xml version="1.0" encoding="UTF-8"?>
<flow ...>
    ...
    <view-state id="foo" view="barView">
    </view-state>
</flow>

and unit test

public void testAssertFooStateHasBarView() {
    ...
    assertCurrentStateEquals("foo");
    assertTrue( getFlowDefinition().getState("confirmation").isViewState());
    // Surely there's an easier way...?
    ViewState viewState = (ViewState)getFlowDefinition().getState("foo");
    View view = viewState.getViewFactory().getView(new MockRequestContext());
    // yuck!
    assertTrue(view.toString().contains("barView"));
}

Is there a simpler way to assert that state foo has view barView ?

You can use this:

assertResponseWrittenEquals("barView", context);

Where context is your MockExternalContext .

This is how I always test this anyway.

如果您实际上是在发信号通知事件,则可以通过以下方法获取ViewSelection并检查名称:

assertViewNameEquals("Your View Name", applicationView(viewSelection));

I can't speak to the rest of your tests, or how to use Webflow, but why are you using contains() to test for equality? I'm sure you don't want a view of "barViewBlah" to match your test, do you?

assertEquals("barView", view.toString());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM