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:

  • First, you can try to provide your own small runtime environment without acutally using an J2EE-Container. One framework that helps you doing that is MockObjects which provides mocks for servlets or JDBC connections.
  • Another approach would be to run your test right within the J2EE container. So we just need some kind of generic interface to invoke that server-side components directly. That is what Jakarta Cactus is providing for you.

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:

  • ADF Controller Runtime
  • ADF Model Runtime
  • ADF Web Runtime
  • Struts Runtime
  • Cactus In-Container Testing

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>
...