Junit testing Struts2 action with Spring -
i want test struts action facing following problem:
severe: [09:16.218] error building bean
it seems spring not dependency injection. indeed, if create default constructor npe because service not instantiated. unit test extend strutsspringtestcase, why action not instanciate correctly ?
here 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; }
unit test :
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 true", result.equals(action.success)); } }
applicationcontext
<bean id="ldapuserservice" class="sword.plateformetest.service.impl.ldapuserserviceimpl" /> <bean id="ldapuserauthaction" scope="prototype" class="sword.plateformetest.action.controllerauthentification"> <constructor-arg ref="ldapuserservice" /> </bean>
and 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>
stack trace :
severe: [56:08.596] error building bean 2013-08-26 13:56:08,600 error sword.plateformetest.action.controllerauthentificationtest.testconnection:37 - error unable intantiate action! @ com.opensymphony.xwork2.defaultactioninvocation.createaction(defaultactioninvocation.java:299) @ com.opensymphony.xwork2.defaultactioninvocation.init(defaultactioninvocation.java:397) @ com.opensymphony.xwork2.defaultactionproxy.prepare(defaultactionproxy.java:194) @ org.apache.struts2.impl.strutsactionproxy.prepare(strutsactionproxy.java:63) @ org.apache.struts2.impl.strutsactionproxyfactory.createactionproxy(strutsactionproxyfactory.java:39) @ com.opensymphony.xwork2.defaultactionproxyfactory.createactionproxy(defaultactionproxyfactory.java:58) @ org.apache.struts2.strutstestcase.getactionproxy(strutstestcase.java:137) @ sword.plateformetest.action.controllerauthentificationtest.testconnection(controllerauthentificationtest.java:34)
you should extended strutsspringjunit4testcase
instead. here's answer it:
how test struts2 action in junit 4 way?
you might have add struts2-junit-plugin dependency.
as @kevin rave, might have add ".do" end of uri. having exact same issue , appending resolved it.
Comments
Post a Comment