views:

479

answers:

3

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?

A: 

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());
matt b
I agree it's gross, that's why I'm looking for a better way. In this case the toString of the View object is [MockViewFactoryCreator.MockView@1b0889a viewId = 'barView'] so I can't do a straight compare and there's no (obvious) API to retrieve that 'barView' value.
Scott Bale
If you cast to MockView, does it give you an accessor for viewName?
matt b
Not an option - MockView is an inner class of MockViewFactoryCreator, which is only package-visible. I could use reflection but that sucks roughly the same amount.
Scott Bale
+1  A: 

You can use this:

assertResponseWrittenEquals("barView", context);

Where context is your MockExternalContext.

This is how I always test this anyway.

TM
Thank you! You have no idea how much hideously ugly code this cleans up.
Scott Bale
+1  A: 

If you are actually signaling events, you can get the ViewSelection and check the name via this method:

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