Here is what I ended up doing:
@Override
protected void onSetUpBeforeTransaction() throws Exception {
LocalizedTextUtil.addDefaultResourceBundle(Constants.BUNDLE_KEY);
ActionContext.getContext().setSession(new HashMap());
MockPortletRequest request = new MockPortletRequest();
MockPortletSession session = new MockPortletSession();
request.setSession(session);
ActionContext.getContext().put(PortletActionContext.REQUEST,
request);
}
Seems to be working properly when combined with my PortletBaseAction. All
of the appfuse generated tests work right out of the box. (So far so good).
YMMV
dashaun wrote:
>
> I'm trying to create a BasePortletActionTestCase because I'm using
> portlets. The portlets work just fine but I think since my actions are
> now extending my PortletBaseAction, that the *ActionTest classes should be
> using a PortletActionContext instead of a ServletActionContext.
>
>
> Here is my PortletBaseAction:
>
> public class PortletBaseAction extends ActionSupport {
> private static final long serialVersionUID = 3525445612504421307L;
> public static final String CANCEL = "cancel";
> protected transient final Log log = LogFactory.getLog(getClass());
> protected String from = null;
> protected String cancel = null;
> protected String delete = null;
> protected String save = null;
> protected MailEngine mailEngine = null;
> protected SimpleMailMessage mailMessage = null;
> protected String templateName = null;
>
> public String cancel() {
> return CANCEL;
> }
>
> @SuppressWarnings("unchecked")
> protected void saveMessage(String msg) {
> List messages = (List) getSession().getAttribute("messages");
>
> if (messages == null) {
> messages = new ArrayList();
> }
>
> messages.add(msg);
> getSession().setAttribute("messages", messages);
> }
>
> @SuppressWarnings("unchecked")
> protected void saveError(String msg) {
> saveMessage(msg);
> }
>
> /**
> * Convenience method to get the Configuration HashMap
> * from the servlet context.
> *
> * @return the user's populated form from the session
> */
> protected Map getConfiguration() {
> Map config = (HashMap) getSession().getPortletContext()
> .getAttribute(Constants.CONFIG);
>
> // so unit tests don't puke when nothing's been set
> if (config == null) {
> return new HashMap();
> }
>
> return config;
> }
>
> protected PortletRequest getRequest() {
> return PortletActionContext.getRequest();
> }
>
> protected String getRemoteUser() {
> return getRequest().getRemoteUser();
> }
>
> protected boolean isUserInRole(String role) {
> return getRequest().isUserInRole(role);
> }
>
> /**
> * Convenience method to get the session.
> *
> * @return the session from the request (request.getSession()).
> */
> protected PortletSession getSession() {
> return getRequest().getPortletSession();
> }
>
> /**
> * Convenience method to set request attribute.
> * @param attrname
> * @param obj
> */
> public void setRequestAttribute(String attrname, Object obj) {
> getRequest().setAttribute(attrname, obj);
> }
>
> public void setMailEngine(MailEngine mailEngine) {
> this.mailEngine = mailEngine;
> }
>
> public void setMailMessage(SimpleMailMessage mailMessage) {
> this.mailMessage = mailMessage;
> }
>
> /**
> * Convenience method for setting a "from" parameter to indicate the
> previous page.
> *
> * @param from indicator for the originating page
> */
> public void setFrom(String from) {
> this.from = from;
> }
>
> public void setDelete(String delete) {
> this.delete = delete;
> }
>
> public void setSave(String save) {
> this.save = save;
> }
> }
>
>
>
> I just modified the BaseAction from Appfuse slightly.
>
> So now all of my Action classes actually extend this class instead of
> BaseAction.
>
> In testing, I figure that I need to change my ActionTest classes as well.
>
> The BaseActionTestCase sets a MockHttpServletRequest in the
> onSetUpBeforeTransaction method
>
> @Override
> protected void onSetUpBeforeTransaction() throws Exception {
> LocalizedTextUtil.addDefaultResourceBundle(Constants.BUNDLE_KEY);
> ActionContext.getContext().setSession(new HashMap());
>
> // change the port on the mailSender so it doesn't conflict with
> an
> // existing SMTP server on localhost
> JavaMailSenderImpl mailSender = (JavaMailSenderImpl)
> applicationContext.getBean("mailSender");
> mailSender.setPort(2525);
> mailSender.setHost("localhost");
>
> // populate the request so getRequest().getSession() doesn't fail
> in BaseAction.java
> ServletActionContext.setRequest(new MockHttpServletRequest());
> }
>
> Well, I'm not using a HttpServletRequest, I'm using a PortletRequest.
>
> In the tests that extend BaseActionTestCase we are setting up
> MockHttpServletRequet objects and placing them into the
> ServletActionContext.
>
> Well, how do I place my MockPortletRequest into the PortletActionContext?
>
> PortletActionContext doesn't have a setRequest method.
>
> What am I missing?
>
> From my BasePortletActionTestCase:
>
> @Override
> protected void onSetUpBeforeTransaction() throws Exception {
> LocalizedTextUtil.addDefaultResourceBundle(Constants.BUNDLE_KEY);
> ActionContext.getContext().setSession(new HashMap());
>
> MockPortletSession session = new MockPortletSession();
> //todo something with PortletActionContext
> }
>
--
View this message in context:
http://www.nabble.com/Struts2-Portlet-Test-tf4734378s2369.html#a13551259
Sent from the AppFuse - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]