在带有Guice的Stripes中使用MockRoundtrip

时间:2011-06-10 16:35:07

标签: java unit-testing guice stripes

我最近将Guice引入了我的ActionBeans,之前actionbean的数据访问层实现是硬编码的。

我有这样的集成测试:

MockServletContext context = TestHelper.getServletContext();
MockRoundtrip trip = new MockRoundtrip(context, LoginActionBean.class);


trip.setParameter("authenticate", "Login");
trip.setParameter("username", "testuser2");
trip.setParameter("password", "testuser2pass");
trip.setParameter("targetUrl", "Activity.action?jobId=1");
trip.execute();

LoginActionBean bean = trip.getActionBean(LoginActionBean.class);

既然我正在使用Guice,我找不到将依赖项注入LoginActionBean的方法,因为我自己从未实际实例化它。

如何告诉guice将依赖项注入此类?

1 个答案:

答案 0 :(得分:1)

您需要使用Guice配置配置MockServletContext,就像在web.xml中一样(可能在 TestHelper getServletContext()中法)。