I've just started testing using MockRoundtrip. My action class adds an error to
the messages. Within my test class I'm trying to ensure the message is written,
but I get a NullPointerException. 

Something like this. 

    MockServletContext servletContext = getServletContext();
    MockRoundtrip trip =  
        new MockRoundtrip(servletContext, MessageWritingActionBean.class); 
    trip.execute();
    MessageWritingActionBean actionBean = 
        trip.getActionBean(MessageWritingActionBean.class);
    ActionBeanContext abContext = actionBean.getContext(); 
    List messages = abContext.getMessages(); 
    // error here            ^^^^^^^^^^^^

Is this the wrong way to go about checking the message or is it a bug in the
testing code ? 

I traced it through the Stripes source and the error is happening in
FlashScope.getContainer(HttpServletRequest req, boolean create) with a
descriptive error message. 

        catch (IllegalStateException ise) {
            // If the session has been invalidated we'll get this exception, but
there's no
            // way to know this without try and getting the exception :(
            log.warn("An IllegalStateException got thrown trying to create a
flash scope. ",
                     "This happens when add something to flash scope for the
first time ",
                     "causes creation of the HttpSession, but for some other
reason the ",
                     "response is already committed!");
            return null;
        }

But not sure how to get around this (or if I should be getting around it). 

Complete code to reproduce follows (JUnit test) 

Daryl 







========================================
Test Class 
========================================
package com.myapp.stripes;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.controller.DispatcherServlet;
import net.sourceforge.stripes.controller.StripesFilter;
import net.sourceforge.stripes.mock.MockRoundtrip;
import net.sourceforge.stripes.mock.MockServletContext;

import org.junit.Assert;
import org.junit.Test;

public class MessageWritingActionBeanTest {

    @Test
    public void testIt() throws Exception {
        MockServletContext servletContext = getServletContext();
        MockRoundtrip trip = new MockRoundtrip(servletContext,
MessageWritingActionBean.class);
        trip.execute();
        MessageWritingActionBean actionBean =
trip.getActionBean(MessageWritingActionBean.class);
        ActionBeanContext abContext = actionBean.getContext();
        List messages = abContext.getMessages();
        Assert.assertNotNull(messages);
        Assert.assertFalse("Message list is not empty.",
actionBean.getContext().getMessages()
                .isEmpty());
    }

    private MockServletContext getServletContext() {
        MockServletContext context = new MockServletContext("test");

        // Add the Stripes Filter
        Map<String, String> filterParams = new HashMap<String, String>();
        filterParams.put("ActionResolver.UrlFilters", "target/classes");
        filterParams.put("Interceptor.Classes",
                "net.sourceforge.stripes.integration.spring.SpringInterceptor,"
                        +
"net.sourceforge.stripes.controller.BeforeAfterMethodInterceptor");
        filterParams.put("ActionBeanContext.Class",
                "net.sourceforge.stripes.action.ActionBeanContext");
        filterParams.put("ActionResolver.PackageFilters", "com.myapp.stripes");
        context.addFilter(StripesFilter.class, "StripesFilter", filterParams);

        // Add the Stripes Dispatcher
        context.setServlet(DispatcherServlet.class, "StripesDispatcher", null);

        return context;
    }

}

========================================
Action Bean Class 
========================================

package com.myapp.stripes;

import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.validation.SimpleError;

public class MessageWritingActionBean implements ActionBean {
    ActionBeanContext context;

    @DefaultHandler
    public Resolution doSomeStuff() {
        SimpleError error = new SimpleError("Writing an error");
        this.getContext().getMessages().add(error);
        return new ForwardResolution("/somePage.jsp");
    }

    public ActionBeanContext getContext() {
        return context;
    }

    public void setContext(ActionBeanContext context) {
        this.context = context;
    }
}






-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Stripes-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-users

Reply via email to