I am writing Unit Tests for Struts Action classes integrated with Spring.
Struts.xml
<action name = "displaySearchCertiSchedulerAction"
class = "certificationSchedulerAction"
method = "displaySearchCertiScheduler">
<result name = "success">/jsp/admin/SearchCertiScheduler.jsp</result>
</action>
applicationContext.xml
<bean name = "certificationSchedulerAction"
class = "com.admin.action.CertificationSchedulerAction"
scope = "request">
<property name = "certificationSchedulerServices"
ref = "certificationSchedulerServices" />
</bean>
JUnit
@SuppressWarnings("rawtypes")
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class BrokerActionTest extends StrutsSpringJUnit4TestCase {
@Test
public void testGetActionProxy() throws Exception {
ActionProxy proxy = getActionProxy("/getEventCategoryAction");
CertificationSchedulerAction schedulerAction =
(CertificationSchedulerAction) proxy.getAction();
assertNotNull(proxy);
}
}
Error:
Tests in error: testGetActionProxy(com.admin.action.BrokerActionTest): Unable to instantiate Action, certificationSchedulerAction, defined for 'getEventCategoryAction' in namespace '/' No Scope registered for scope 'request'
This works fine if I don't explicitly specify the scope, but it gives error if I specify the scope.
The request scope (and its handlers) is registered by a WebApplicationContext implementation. If you're going to test in a web environment, you'll need @WebAppConfiguration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With