You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by dusty <du...@yahoo.com> on 2009/03/09 20:41:05 UTC

Convention Plugin and Unit Tests

Hi All (err Musachy),

I have the classes below that I use for unit testing. They are a
spin-off/update of the Depressed Programmers unit test class.  I like them
because they can test the Struts config along with the actions themselves +
interceptor execution.  Things go well when you have an @Result annotation
for your action, but when you rely on Convention finding your view jsp by
"convention" my test doesn't seem to think the result is registered in the
Struts config.  

So here is the Controller code (w/o @Result):

@Namespace("/customers")
public class CustomerController extends RootAction implements Preparable {
    private CustomerManager customerManager;
    private Long id;
    private Customer customer;
    private List<Customer> customers;

    @Action("index")
    public String index(){
        customers = customerManager.getAll();
        return "index";
    }


Here is the base class for unit tests that gets Struts setup.  An
ActionProxy is created and there is a setting telling it to executeResult or
not.  If this is false then we are good, but then you are not testing the
config.  When its set to true it can't find the result when no @Result
annotation is present:

@ContextConfiguration(loader=StrutsContextLoader.class , locations =
{"classpath:/applicationContext-resources.xml","classpath:/applicationContext-service.xml","classpath:/applicationContext-CrowdClient.xml","classpath:/applicationContext.xml","/WEB-INF/security.xml"
})
@TransactionConfiguration(transactionManager =
"transactionManager",defaultRollback = true)
@Transactional
public class BaseStrutsTestCaseSpring extends
AbstractTransactionalJUnit4SpringContextTests{
    protected MockHttpServletRequest request;
    protected MockHttpServletResponse response;
    private Dispatcher dispatcher;
    protected ActionProxy proxy;
    WebApplicationContext context;

    @Before
    public void onSetUpBeforeTransaction() throws Exception {
        request = new MockHttpServletRequest();
        response = new MockHttpServletResponse();
        context = (WebApplicationContext)applicationContext;        
        HashMap params = new HashMap();
        params.put("actionPackages", "com.jmh.employeedb.webapp.action");
        dispatcher = new Dispatcher(context.getServletContext(), params);
        dispatcher.init();
        Dispatcher.setInstance(dispatcher);

    }

    protected <T> T createAction(String namespace, String name)
            throws Exception {
        Map extraContext = dispatcher.createContextMap(request, response,
null, context.getServletContext());
        proxy =
dispatcher.getContainer().getInstance(ActionProxyFactory.class).
                createActionProxy(
                        namespace, name, null,extraContext, true, false);
        proxy.getInvocation().getInvocationContext().
                setSession(new HashMap());
        proxy.setExecuteResult(true);
        ServletActionContext.setContext(
                proxy.getInvocation().getInvocationContext());
        ServletActionContext.setServletContext(context.getServletContext());
        return (T) proxy.getAction();
    }

Here is the moving part of CustomContextLoader class referenced in the
annotation:

public class StrutsContextLoader extends AbstractContextLoader {

    public ApplicationContext loadContext(String... locations) throws
Exception {
        MockServletContext servletContext = new
MockServletContext("/src/main/webapp",new FileSystemResourceLoader());
        servletContext.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,
arrayToString(locations, ","));
        return (new
ContextLoader()).initWebApplicationContext(servletContext);
    }


Finally, here is a test implementation:

public class CustomerControllerTest extends BaseStrutsTestCaseSpring {

    @Test
    public void getCustomerIndex() throws Exception{
        CustomerController customerController =
createAction("/customers","index");
        String result = proxy.execute();
        Assert.assertEquals("index", result);
        Assert.assertNotNull(customerController.getCustomers());
        Assert.assertTrue(customerController.getCustomers().size() > 0);
    }

If I add an @Result annotation to the @Action annotation for index() then
its all good, but then I don't get cool implicit results by convention.  I
assume it has something to do with how I am initializing the Struts
config/dispatcher but I can't be sure.

Any ideas?
-- 
View this message in context: http://www.nabble.com/Convention-Plugin-and-Unit-Tests-tp22417780p22417780.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Convention Plugin and Unit Tests

Posted by Musachy Barroso <mu...@gmail.com>.
It is hard to say, but it could be as simple as
DefaultResultMapBuilder not finding the files where it expects them.
Set some breakpoints there, and I am sure you will find what is
missing.

musachy

On Mon, Mar 9, 2009 at 3:41 PM, dusty <du...@yahoo.com> wrote:
>
> Hi All (err Musachy),
>
> I have the classes below that I use for unit testing. They are a
> spin-off/update of the Depressed Programmers unit test class.  I like them
> because they can test the Struts config along with the actions themselves +
> interceptor execution.  Things go well when you have an @Result annotation
> for your action, but when you rely on Convention finding your view jsp by
> "convention" my test doesn't seem to think the result is registered in the
> Struts config.
>
> So here is the Controller code (w/o @Result):
>
> @Namespace("/customers")
> public class CustomerController extends RootAction implements Preparable {
>    private CustomerManager customerManager;
>    private Long id;
>    private Customer customer;
>    private List<Customer> customers;
>
>    @Action("index")
>    public String index(){
>        customers = customerManager.getAll();
>        return "index";
>    }
>
>
> Here is the base class for unit tests that gets Struts setup.  An
> ActionProxy is created and there is a setting telling it to executeResult or
> not.  If this is false then we are good, but then you are not testing the
> config.  When its set to true it can't find the result when no @Result
> annotation is present:
>
> @ContextConfiguration(loader=StrutsContextLoader.class , locations =
> {"classpath:/applicationContext-resources.xml","classpath:/applicationContext-service.xml","classpath:/applicationContext-CrowdClient.xml","classpath:/applicationContext.xml","/WEB-INF/security.xml"
> })
> @TransactionConfiguration(transactionManager =
> "transactionManager",defaultRollback = true)
> @Transactional
> public class BaseStrutsTestCaseSpring extends
> AbstractTransactionalJUnit4SpringContextTests{
>    protected MockHttpServletRequest request;
>    protected MockHttpServletResponse response;
>    private Dispatcher dispatcher;
>    protected ActionProxy proxy;
>    WebApplicationContext context;
>
>    @Before
>    public void onSetUpBeforeTransaction() throws Exception {
>        request = new MockHttpServletRequest();
>        response = new MockHttpServletResponse();
>        context = (WebApplicationContext)applicationContext;
>        HashMap params = new HashMap();
>        params.put("actionPackages", "com.jmh.employeedb.webapp.action");
>        dispatcher = new Dispatcher(context.getServletContext(), params);
>        dispatcher.init();
>        Dispatcher.setInstance(dispatcher);
>
>    }
>
>    protected <T> T createAction(String namespace, String name)
>            throws Exception {
>        Map extraContext = dispatcher.createContextMap(request, response,
> null, context.getServletContext());
>        proxy =
> dispatcher.getContainer().getInstance(ActionProxyFactory.class).
>                createActionProxy(
>                        namespace, name, null,extraContext, true, false);
>        proxy.getInvocation().getInvocationContext().
>                setSession(new HashMap());
>        proxy.setExecuteResult(true);
>        ServletActionContext.setContext(
>                proxy.getInvocation().getInvocationContext());
>        ServletActionContext.setServletContext(context.getServletContext());
>        return (T) proxy.getAction();
>    }
>
> Here is the moving part of CustomContextLoader class referenced in the
> annotation:
>
> public class StrutsContextLoader extends AbstractContextLoader {
>
>    public ApplicationContext loadContext(String... locations) throws
> Exception {
>        MockServletContext servletContext = new
> MockServletContext("/src/main/webapp",new FileSystemResourceLoader());
>        servletContext.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,
> arrayToString(locations, ","));
>        return (new
> ContextLoader()).initWebApplicationContext(servletContext);
>    }
>
>
> Finally, here is a test implementation:
>
> public class CustomerControllerTest extends BaseStrutsTestCaseSpring {
>
>    @Test
>    public void getCustomerIndex() throws Exception{
>        CustomerController customerController =
> createAction("/customers","index");
>        String result = proxy.execute();
>        Assert.assertEquals("index", result);
>        Assert.assertNotNull(customerController.getCustomers());
>        Assert.assertTrue(customerController.getCustomers().size() > 0);
>    }
>
> If I add an @Result annotation to the @Action annotation for index() then
> its all good, but then I don't get cool implicit results by convention.  I
> assume it has something to do with how I am initializing the Struts
> config/dispatcher but I can't be sure.
>
> Any ideas?
> --
> View this message in context: http://www.nabble.com/Convention-Plugin-and-Unit-Tests-tp22417780p22417780.html
> Sent from the Struts - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>



-- 
"Hey you! Would you help me to carry the stone?" Pink Floyd

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org