Java Reference
In-Depth Information
Joe Asks. . .
What If I Prefer TestNG?
I'm using JUnit for the automated testing examples, but this is
just an arbitrary choice. TestNG is also popular (in fact, it is used
to test Stripes itself). If you prefer TestNG (or any other testing
framework, for that matter), you can use it instead of JUnit sim-
ply by replacing the libraries and adapting the test code. The
Stripes mock objects have no dependencies on any particular
testing framework. In fact, they are not even tied to Stripes; you
could technically use them to test independent servlet con-
tainer artifacts, such as external filters and servlets.
2. If our tests require the use of a session, create a MockHttpSession
object.
3. Use a MockRoundtrip object to simulate a request (link with or with-
out parameters, form submission, . . . ) to an action bean, and ver-
ify the results. If we created a MockHttpSession object, attach it to
MockRoundtrip .
Let's write a couple of automated tests for the contact form as an exam-
ple. We begin with a method marked as @BeforeClass so that JUnit will
run it only once, before running all the @Test methods.
Download email_26/src/stripesbook/test/stripesmock/ContactFormActionBeanTest.java
package stripesbook.test.stripesmock;
public class ContactFormActionBeanTest {
private static MockServletContext mockServletContext;
private static MockHttpSession mockSession;
@BeforeClass
public static void setup() throws Exception {
mockServletContext = new MockServletContext("webmail");
Map<String,String> params = new HashMap<String,String>();
params.put("ActionResolver.Packages", "stripesbook.action");
params.put("Extension.Packages", "stripesbook.ext,"
+ "org.stripesstuff.stripersist");
Ê
mockServletContext.addFilter(StripesFilter. class ,
"StripesFilter", params);
mockServletContext.setServlet(DispatcherServlet. class ,
"DispatcherServlet", null );
Ë
mockSession = new MockHttpSession(mockServletContext);
}
 
 
Search WWH ::




Custom Search