使用丰富的上下文测试Webflow

时间:2015-09-07 16:33:46

标签: unit-testing spring-webflow

可以在Spring的@WebAppConfiguration中测试Webflow,所以我不必创建模拟服务bean等吗?

特别是,测试中的webflow(简洁)documentation没有注意到这一点,并建议使用存根类来测试服务。这可能适用于非常精细的单元测试,但不适用于集成测试或更多增量单元测试。

1 个答案:

答案 0 :(得分:0)

尽管没有记录,但它确实有效并且有用。使用Spring的@WebAppConfiguration并使用webflow注册服务。请注意,不会仅仅因为@WebAppConfiguration找到服务而调用服务。它们也需要在webflow测试中注册。

@RunWith(SpringJUnit4ClassRunner.class)

/**Use @WebAppConfiguration with path specified from maven's root to webapp root. 
 * Extend AbstractXmlFlowExecutionTests as per Webflow's documentation***/
@WebAppConfiguration("/WebContent")
@ContextHierarchy(
{ @ContextConfiguration(locations =
    { "classpath:AssessmentAdminTL-servlet.xml" }) })
public class TestFlowTest extends AbstractXmlFlowExecutionTests
{

@Autowired
private BusinessService businessService;

@Autowired
private AdminUserViewService adminUserViewService;

@Autowired
ApplicationContext context;

/**Specify flow config location***/
@Override
protected FlowDefinitionResource getResource(FlowDefinitionResourceFactory resourceFactory)
{
    return resourceFactory.createFileResource("WebContent/WEB-INF/flows/admin/flow-test.xml");
}

/**Register beans**/
@Override
protected void configureFlowBuilderContext(MockFlowBuilderContext builderContext)
{
    builderContext.registerBean("businessService", businessService);
    builderContext.registerBean("adminUserViewService", adminUserViewService);
}

/**Run Tests**/
@Test
public void testTestFlowEntryTest()
{
    try
    {
        TestSS.getLogger().debug("testTestFlowEntry ....");
        MutableAttributeMap input = new LocalAttributeMap();
        MockExternalContext econtext = new MockExternalContext();

        startFlow(input, econtext);

        assertCurrentStateEquals("hellotlv1");

        TestSS.getLogger().debug("testTestFlowEntry done");

    }
    catch (Exception e)
    {
        SS.getLogger().error(e.getMessage(), e);
        fail();

    }
}

@Test
public void testLoginSuccessTest()
{
    try
    {
    setCurrentState("xzlogin");
    MutableAttributeMap input = new LocalAttributeMap();
    ViewWrapperUserAdminLogin vwLogin = new ViewWrapperUserAdminLogin(new UserAdmin());
    vwLogin.setBusinessId(new Long(1));
    vwLogin.setUserName("jake0");
    vwLogin.setPassword("1234");
    vwLogin.setPin(1234);

    getFlowScope().put("userAdminForm", vwLogin);


    MockExternalContext context = new MockExternalContext();
    context.setEventId("dologin");
    resumeFlow(context);

    assertFlowExecutionEnded();
    assertFlowExecutionOutcomeEquals("xzhome");
    }
    catch(Exception e)
    {
        SS.getLogger().error(e.getMessage(),e);
        fail();
    }
}

}