|
Ihr Spezialist für komplexe IT-Systeme
|
|
J2EE is a quite complex environment. Most of the time you only program components (like EJBs or Servlets) that get executed within a special runtime environment. So your components get some input objects (like an HTTP Request) and return output objects (e.g. an HTTP Response). Additionally most of your components do some backend job and do not provide a normal user interface. So dealing with this components during development and testing turns out to be a little bit more difficult.
There are two different concepts on how people try to make that job a little bit easier:
To show you how it is down in practice I will give an example in using Cactus to do in-container testing for the ADF ToyStore.
The example will be to test the handling of invalid userlogins (e.g. a wrong password). As shown in the screen shot above, we need to check if we get the correct error message displayed (Struts Action Error) and that we are forwareded to the correct JSP-View (Struts ActionForward).
The picture above shows the steps performed during running the Cactus In-Container Tests:
First we have to get Cactus and StrutsTestCase into JDeveloper and the ADF ToyStore Project. Please download and extract the packages of both tools to a location on your computer. Next you have to include the JAR-Files with JDeveloper, to do that go to Tools -> Manage Libraries and create an new library (I called mine "Cactus In-Container Testing") and add all the JAF-Files to the Classpath of that library as shown in the following screen shot:
We will extend the Testing and ToyStoreViewController Projects and before we can do that, we have to adjust the Project Settings for both.
We will include MyCactusTestCase to the Testing project, so add the following libraries to make it compile with the new TestCase:
The cactus.properties is also need, create a new file directly below Application Sources for that, containing something like:
cactus.contextURL=http://192.168.1.70:8990/ADFToyStore cactus.servletRedirectorName=ServletRedirector cactus.enableLogging=true
For the ToyStoreViewController project we also need to add the library for "Cactus In-Container Testing". To get the classes form the Testing project into the J2EE container we have to make the ToyStoreViewController project dependent on the Testing project. See the following screen shot for that:
Finally we have to add some sections to the web.xml. These entries register the ServletRedirector of Cactus with OC4J and the additional filter-mapping makes sure that we acquire an ADF Binding Context for the TestCases.
...
<filter-mapping>
<filter-name>ADFBindingFilter</filter-name>
<servlet-name>ServletRedirector</servlet-name>
</filter-mapping>
...
<servlet>
<servlet-name>ServletRedirector</servlet-name>
<servlet-class>org.apache.cactus.server.ServletTestRedirector</servlet-class>
</servlet>
...
<servlet-mapping>
<servlet-name>ServletRedirector</servlet-name>
<url-pattern>/ServletRedirector</url-pattern>
</servlet-mapping>
...
The last and funny part is to implement the CactusStrutsTestCase in an similar way as you would implement an normal JUnit-TestCase. Create the following class below Testing -> toystore.test.unittest:
package toystore.test.unittests;
import javax.servlet.http.HttpSession;
import oracle.adf.controller.struts.forms.BindingContainerActionForm;
import oracle.adf.model.BindingContext;
import oracle.adf.model.servlet.HttpBindingContext;
import org.apache.struts.action.ActionServlet;
import servletunit.struts.CactusStrutsTestCase;
public class MyCactusTestCase extends CactusStrutsTestCase {
public MyCactusTestCase(String testName) {
super(testName);
}
public void testFailedSignin() {
addRequestParameter("event_verifySignin", "foo");
addRequestParameter("username","frank");
addRequestParameter("password","tiger");
setRequestPathInfo("/signin");
actionPerform();
verifyInputForward();
verifyForwardPath("/WEB-INF/jsp/signin.jsp");
verifyActionErrors(new String[] {"signin.error.logininvalid"});
}
public static void main(String[] args) {
junit.textui.TestRunner.run(MyCactusTestCase.class);
}
}
That is all you have to do, now have fun with running your new TestCase.