|
Information from the servlet context
The servlet context contains information about the servlet container and data
from the web.xml file. Let's see how STC identifies itself:
ServletContext ctx = getActionServlet().getServletContext();
System.out.println(ctx.getServerInfo());
System.out.println ("Supports Servlet API " +
ctx.getMajorVersion() + "." +
ctx.getMinorVersion());
|
The output from this bit of code is:
MockServletEngine/1.9.5
Supports Servlet API 2.3
|
It's simple to add a parameter to the servlet context. Again,
we use a simulator class:
ServletContextSimulator context = (ServletContextSimulator)ctx;
context.setInitParameter("parm1", "value1");
|
Among the files you may download, there's a program called
MockStrutsTestCaseSimulation
that gives several examples on how to modify the simulated objects.
One-time set up in JUnit
A common "problem" when you're coding test cases for Struts actions
is that you need some kind of initializations to be carried out before you can
start your testing. JUnit has the setUp method for this purpose, but it's
called for every test-method in your test class. This may add some
unwanted overhead to your program, and there is a way around the problem.
Use a JUnit suite method with an inner class construction like this
(the class name is MockOneTimeSetup):
public static Test suite() {
TestSuite suite = new TestSuite(MockOneTimeSetup.class);
TestSetup wrapper = new TestSetup(suite) {
public void setUp() {
// code your set up here
}
public void tearDown() {
// code your tear down here
}
};
return wrapper;
}
|
It's important to note, however, that the suite method is static. So
if you want to save some data, use static variables and fetch them from the
ordinary JUnit setUp method. Here's an example where a list of DVDs are fetched
once, saved in static storage, and then--for each textXXX-method--saved
in session scope:
private static DVDManager dvds;
public void setUp() throws Exception {
super.setUp();
getSession().setAttribute("dvds", dvds);
}
public static Test suite() {
TestSuite suite = new TestSuite(MockOneTimeSetup.class);
TestSetup wrapper = new TestSetup(suite) {
public void setUp() {
dvds = new DVDManager();
}
public void tearDown() {
// nothing
}
};
return wrapper;
}
. . .
|
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|