在Stripes框架中,如何在使用MockRoundtrip时依赖注入操作bean中的字段?

时间:2013-02-12 18:51:28

标签: java dependency-injection stripes

以下是设置Stripes以使用MockRoundtrip的示例:

private void setupStripes() {
    context = new MockServletContext("testresults");

    // Add the Stripes Filter
    Map<String,String> filterParams = new HashMap<String,String>();
    filterParams.put("ActionResolver.Packages", "com.test.project.action,com.test.results.action");
    context.addFilter(StripesFilter.class, "StripesFilter", filterParams);

    // Add the Stripes Dispatcher
    context.setServlet(DispatcherServlet.class, "StripesDispatcher", null);


    mockRoundtrip = new MockRoundtrip(context, MyActionBean.class);
    assertNotNull(mockRoundtrip.getActionBean(MyActionBean.class));
}

问题是最后一行始终无法断言。您必须先调用mockroundtrip.execute("eventName"),但到那时,您的动作bean已经为您创建了。如果您的操作bean执行依赖于appserver的操作(例如:使用EJB,执行JNDI查找),则永远无法完成execute()的调用。我希望能够做的是获取我的动作bean实例,依赖注入其字段,然后然后调用execute()。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:1)

我在邮件列表上发布了这个并得到了这个答案:

  

显然这个人使用拦截器进行测试。

     

我认为这也是你想要的。编写一个Stripes Interceptor,可以根据需要执行“注入”操作,并且只为您的单元测试注册一个。您不必存根任何IMO。

     

顺便说一下@SpringBean的作用:它也基于拦截。

总之,创建一个只在运行测试代码时才使用的拦截器。这个拦截器可以依赖于Inject您正在测试的动作bean。