Hi S,
Yes, I think your code would work ... *except* that, as you mentionned,
getServlet() is deprecated with *no* replacement. Here is an extract from
the javadoc:
"This method was originally defined to retrieve a servlet from a
ServletContext. In this version, this method always returns null and remains
only to preserve binary compatibility. This method will be permanently
removed in a future version of the Java Servlet API.
In lieu of this method, servlets can share information using the
ServletContext class and can perform shared business logic by invoking
methods on common non-servlet classes."
In other words, it will not work .... However, writing:
"
MyAction action = new MyAction();
action.setServlet((new ActionServlet()).init(config));
"
is probably fine too. It is also simpler as you don't need the mapping for
ActionServlet in your web.xml file. You do need struts-config.xml though.
Hope it helps. Thanks.
-Vincent
----- Original Message -----
From: <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, August 02, 2001 12:12 PM
Subject: Testing Struts components?
> I'm trying to use Cactus to test my Struts components. Being quite new to
> unit testing
> in general and Cactus in particular I'm wondering why I have to create an
> instance of
> org.apache.struts.action.ActionServlet when one is already present in the
> web container.
> Currently, I use the approach below (thanks to those who posted their
> source code :)).
>
> I'm aware that "ServletContext.getServlet is deprecated but shouldn't I be
> able to
> do something along the lines of:
>
> [...]
> public void testLoginAction()
> throws java.io.IOException, javax.servlet.ServletException {
>
> ServletContext sc = config.getServletContext();
> ActionServlet as (ActionServlet)sc.getServlet("action");
> [...]
>
> and get at the ActionServlet already running?
>
>
> Thanks in advance.
> S. Bro
>
>
>
> --------------------------------------------------------------------------
-------
> [webapp.xml]
>
> ...
>
> <servlet>
> <name>action</name>
> <description>Action Servlet</description>
> <code>org.apache.struts.action.ActionServlet</code>
> <servlet-path>*.do</servlet-path>
> <autostart>true</autostart>
> <init-parameter>
> <name>application</name>
> <value>ApplicationResources</value>
> </init-parameter>
> <init-parameter>
> <name>config</name>
> <value>/WEB-INF/struts-config.xml</value>
> </init-parameter>
> <init-parameter>
> <name>debug</name>
> <value>2</value>
> </init-parameter>
> <init-parameter>
> <name>detail</name>
> <value>2</value>
> </init-parameter>
> <init-parameter>
> <name>validate</name>
> <value>true</value>
> </init-parameter>
> </servlet>
>
>
> <servlet>
> <name>ServletRedirector</name>
> <description>Cactus Testing ServletRedirector</description>
> <code>org.apache.commons.cactus.server.ServletTestRedirector</code>
> <servlet-path>/ServletRedirector</servlet-path>
> <autostart>false</autostart>
> <init-parameter>
> <name>application</name>
> <value>ApplicationResources</value>
> </init-parameter>
> <init-parameter>
> <name>debug</name>
> <value>2</value>
> </init-parameter>
> <init-parameter>
> <name>detail</name>
> <value>2</value>
> </init-parameter>
> <init-parameter>
> <name>validate</name>
> <value>true</value>
> </init-parameter>
> </servlet>
>
> ...
>
> [/webapp.xml]
> --------------------------------------------------------------------------
-------
>
>
>
> --------------------------------------------------------------------------
-------
> [TestActionServlet]
>
> import javax.servlet.ServletException;
> import javax.servlet.ServletConfig;
> import javax.servlet.http.HttpServletRequest;
> import org.apache.struts.action.*;
>
>
> /**
> * Insert the type's description here.
> * Creation date: (02-08-01 11:16:47)
> * @author: Administrator
> */
> class TestActionServlet extends org.apache.struts.action.ActionServlet {
>
> TestActionServlet(ServletConfig cfg)
> throws ServletException
> {
> init(cfg);
> }
> public Action createAction(ActionMapping am, HttpServletRequest req)
{
> return processActionCreate(am, req);
> }
> public ActionForm createActionForm(ActionMapping am,
> HttpServletRequest req) {
> return processActionForm(am, req);
> }
> }
>
> [/TestActionServlet]
> --------------------------------------------------------------------------
-------
>
>
>
> --------------------------------------------------------------------------
-------
> [ActionTest]
>
> import javax.servlet.*;
> import javax.servlet.http.*;
> import org.apache.struts.action.*;
> import junit.framework.*;
> import org.apache.commons.cactus.*;
>
> import com.acme.mywebapp.common.*;
> import com.acme.mywebapp.login.*;
>
> /**
> * Insert the type's description here.
> * Creation date: (02-08-01 10:45:36)
> * @author: Administrator
> */
> public class ActionTest extends org.apache.commons.cactus.ServletTestCase
{
>
> private TestActionServlet as;
>
> public ActionTest(String name) {
> super(name);
> }
>
> public static void main(String[] args) {
> junit.textui.TestRunner.run(suite());
> }
>
> public static junit.framework.Test suite() {
> return new TestSuite(ActionTest.class);
> }
>
> public void testLoginAction()
> throws java.io.IOException, javax.servlet.ServletException {
>
> as = new TestActionServlet(config);
>
> ActionMapping am = as.findMapping("/login");
> Action a = as.createAction(am, request);
> ActionForm form = as.createActionForm(am, request);
> ActionForward af = a.perform(am, form, request, response);
>
> User user = (User) session.getAttribute(ISessionKeys.USER_KEY);
>
> assertTrue("No user stored in session.", user != null);
> assertTrue(
> "LoginAction did not forward to success",
> af.getName().equals("success"));
> }
>
> }
>
> [/ActionTest]
> --------------------------------------------------------------------------
-------
>
>