In Spring Webflow unit test, how do you assert that a view state has a view of a given name?
-
18-09-2019 - |
Question
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
?
Solution
You can use this:
assertResponseWrittenEquals("barView", context);
Where context
is your MockExternalContext
.
This is how I always test this anyway.
OTHER TIPS
If you are actually signaling events, you can get the ViewSelection and check the name via this method:
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());