我想测试 Struts 动作,但我面临以下问题:
SEVERE: [09:16.218] Error building bean
看来Spring不做依赖注入。事实上,如果我创建一个默认构造函数,我会得到一个 NPE,因为该服务没有被实例化。我的单元测试扩展了 StrutsSpringTestCase,那么为什么我的 Action 没有正确实例化?
这是我的行动:
public ControllerAuthentification(LdapUserService service) {
this.ldapUserService = service;
}
public String connection() {
if (userLdap != null) {
ldapUserService.save(userLdap);
addActionMessage(getText("success.connect"));
return SUCCESS;
}
return INPUT;
}
单元测试 :
public class ControllerAuthentificationTest extends StrutsSpringTestCase {
public void testConnection() throws Exception {
request.setParameter("userLdap.login", "login");
request.setParameter("userLdap.password", "password");
proxy = getActionProxy("/connection");
ControllerAuthentification action = (ControllerAuthentification) proxy.getAction();
String result = proxy.execute();
assertTrue("it would be true", result.equals(Action.SUCCESS));
}
}
应用上下文
<bean id="ldapUserService" class="sword.plateformetest.service.impl.LdapUserServiceImpl" />
<bean id="ldapUserAuthAction" scope="prototype"
class="sword.plateformetest.action.ControllerAuthentification">
<constructor-arg ref="ldapUserService" />
</bean>
和我的 web.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
堆栈跟踪 :
SEVERE: [56:08.596] Error building bean
2013-08-26 13:56:08,600 ERROR sword.plateformetest.action.ControllerAuthentificationTest.testConnection:37 - error
Unable to intantiate Action!
at com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:299)
at com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:397)
at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:194)
at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
at org.apache.struts2.StrutsTestCase.getActionProxy(StrutsTestCase.java:137)
at sword.plateformetest.action.ControllerAuthentificationTest.testConnection(ControllerAuthentificationTest.java:34)