Vishwanath Krishnamurthi's blog

A blog on Java EE, clean code, open source and TDD

Using Mockito to unit test Spring Webflow

with 6 comments

The flows in Spring Webflow act as powerful controllers. And it is very crucial in a webapp to have the controllers tested.

Unit testing Spring Webflow becomes all the way easier, when a mocking framework is used. And I find Mockito to be pretty good in that arena.

Without much talk, here goes some code:

Step1:
To extend AbstractXmlFlowExecutionTests.

Let’s locate the flow-under-test as such

@Override
 protected FlowDefinitionResource getResource(
 FlowDefinitionResourceFactory resourceFactory) {
 FlowDefinitionResource resource = resourceFactory.createResource("foo-flow.xml");
 Assert.notNull(resource);
 return resource;
 }

Step2:

Mock away the beans used in flow


BazService mockedBazService;

@Override
 protected void registerMockFlowBeans(ConfigurableBeanFactory flowBeanFactory){
 mockedBazService=mock(BazService.class);
flowBeanFactory.registerSingleton("bazService",mockedBazService);
 flowBeanFactory.registerSingleton("barService", mock(BarService.class));
 flowBeanFactory.registerSingleton("MyExceptionHandler", mock(MyExceptionHandler.class));
 }
}

Note that I’m holding the reference for BazBean so I can do some stubbing using Mockito later..
Step3:
Then lets code up a test.

 MockExternalContext context = new MockExternalContext();
 @Test
 public void testFlowShouldEnterStartState()
 {
  this.startFlow(context);
  assertCurrentStateEquals("myFirstState");

 }

How about triggering an event, and checking a transition ?

@Test
 public void testStateWhenFooBarProcessingEventTriggered()
 {

   this.startFlow(context);
   context.setEventId("startProcessingFooBar");
   resumeFlow(context);
   assertCurrentStateEquals("fooBarProcessor");
 }

Some stubbing..

 @Test
 public void testProcessingDetails()
 {
    this.startFlow(context);
    when(mockedBazService
     .isDetailsAvailable(anyString())
     ).thenReturn(true);
    context.setEventId("processDetails");
    resumeFlow(context);
    assertCurrentStateEquals("processDetails");

 }

Enough to get started with testing webflows, I guess. I’ll update more later 🙂

Update:

Some more observations on SWF unit testing.

Advertisements

Written by Vishwanath Krishnamurthi

July 12, 2011 at 10:59 am

6 Responses

Subscribe to comments with RSS.

  1. Thanks Vishwanath, this example has been a big help to me. Sarah W

    Sarah

    July 21, 2011 at 12:34 pm

    • Hi Sarah,

      Glad to know that it was helpful. I have put up another post on this, with a few other observations. Might help if you are exploring more. -Vishwa

  2. any chance you could paste the whole file in here ? I’m a bit confused.

    Ive created a test that extends AbstractXmlFlowExecutionTests, but when I run it, the getResource() method never seems to get called.

    Do I have to call it explicitly ?

    thanks

    ew

    July 22, 2011 at 9:07 am

    • I am guessing that the method might not be properly overriden. No, we won’t need to call getResource() explicitly. Have you annotated the method with @Override.. So we’d know if that’s what is causing the problem. I don’t have the code with me, but if you could link me to your code gist ( https://gist.github.com/ ) I can take a look..

  3. […] Vishwanath Krishnamurthi’s blog […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: