M
Ara Abrahamian wrote:
public void testSuccessfulLogin() throws Exception { parameters.put("username", new String[]{"savaki"}); parameters.put("password", new String[]{"password"}); ActionProxy proxy = ActionProxyFactory.getFactory(). createActionProxy("/member", "login", extraContext, false); String result = proxy.execute(); assertEquals(Action.SUCCESS, result);
LoginAction action = (LoginAction)proxy.geAction(); // do more assertions here }
Programmable ActionProxy is really awesome! Combined with a custom ConfigurationProvider object you can test the action very easily, you can just enable the ValidationInterceptor for example and easily unit test all your externalized validation.xml settings too!
We created an abstract base class for all xwork tests. It configures an ActionProxy object. ValidationInterceptor is added to the list of interceptors and a MockResult is installed as the only result type.
public abstract class AbstractXWorkTest extends TestCase { private ActionProxy proxy; private Class actionClass;
public AbstractXWorkTest( Class actionClass ) { this.actionClass = actionClass; }
protected void setUp() throws Exception { setUp(actionClass, actionClass.getName(), null); }
/** * Use this method if you want to setup this test with a specific action class and method. * * This will throw out all previous configuration done by the normal setUp() method. */ protected void setUp(Class clazz, String alias, String method) throws Exception { ConfigurationManager.addConfigurationProvider( createConfigurationProvider( method, alias ) ); ConfigurationManager.getConfiguration().reload(); proxy = ActionProxy.createActionProxy( "", alias, new HashMap() ); }
protected TestConfigurationProvider createConfigurationProvider( String method, String alias ) { return new TestConfigurationProvider(method, alias); }
protected void tearDown() throws Exception { ConfigurationManager.clearConfigurationProviders(); super.tearDown(); }
protected Action getAction() { return proxy.getAction(); }
protected ActionProxy getActionProxy() { return proxy; }
protected class TestConfigurationProvider implements ConfigurationProvider { private String methodName; private String alias;
public TestConfigurationProvider(String methodName, String alias) { this.methodName = methodName; this.alias = alias; }
public void init( Configuration config ) { PackageConfig defaultPackageConfig = new PackageConfig();
HashMap results = createResultTypes(); List interceptors = createInterceptors();
ActionConfig fooActionConfig = new ActionConfig( methodName, actionClass, new HashMap(), results, interceptors ); defaultPackageConfig.addActionConfig( alias, fooActionConfig );
config.addPackageConfig( "defaultPackage", defaultPackageConfig ); }
protected HashMap createResultTypes() { HashMap results = new HashMap(); ResultConfig resultConfig = new ResultConfig( "def-result-type", MockResult.class, new HashMap() ); results.put( Action.SUCCESS, resultConfig ); return results; }
protected List createInterceptors() { List interceptors = new ArrayList(); interceptors.add( new ValidationInterceptor() ); return interceptors; }
public void destroy() { }
public boolean needsReload() { return false; } } }
Just do this to test a TestCreatePageAction derived test case do something like this:
assertEquals( Action.SUCCESS, getActionProxy().execute() ); assertEquals( 1, getCreatePageAction().getActionErrors().size() ); assertEquals( getCreatePageAction().getText( "space.key.empty" ), getCreatePageAction().getActionErrors().iterator().next() ); assertEquals( 0, getCreatePageAction().getFieldErrors().size() );
That's it. Hope it was useful for those who were thinking about unit testing their xwork action classes :-)
Ara.
------------------------------------------------------- This SF.Net email sponsored by: Free pre-built ASP.NET sites including Data Reports, E-commerce, Portals, and Forums are available now. Download today and enter to win an XBOX or Visual Studio .NET. http://aspnet.click-url.com/go/psa00100006ave/direct;at.asp_061203_01/01 _______________________________________________ Opensymphony-webwork mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/opensymphony-webwork
------------------------------------------------------- This SF.Net email sponsored by: Free pre-built ASP.NET sites including Data Reports, E-commerce, Portals, and Forums are available now. Download today and enter to win an XBOX or Visual Studio .NET. http://aspnet.click-url.com/go/psa00100006ave/direct;at.asp_061203_01/01 _______________________________________________ Opensymphony-webwork mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/opensymphony-webwork