Java Reference
In-Depth Information
• Checking the presence or absence of validation errors
• Confirming that type conversion is working properly
• Verifying which URL is being returned
• Testing URL binding
We'll start with a testing framework, JUnit, available at http://www.junit.
org . You can run JUnit in a variety of ways: with Ant, with your IDE,
or as a stand-alone program. You'll find that the topic's source code
bundle is set up to run the tests with Ant. No matter how you run the
tests, the code remains the same.
For example, the following is a “Hello, World!” test: annotating a method
with @Test tells JUnit to run it as a test, and the Assert class contains
methods to test for many different kinds of conditions. 7
Download email_26/src/stripesbook/test/basic/HelloWorldTest.java
package stripesbook.test.basic;
import org.junit.Test;
import static org.junit.Assert. * ;
public class HelloWorldTest {
@Test
public void testHello() {
String expected = "HELLO";
String result = "hello".toUpperCase();
assertEquals(expected, result);
}
}
With that minimal introduction to JUnit, we're now ready to write some
Stripes test code.
Testing with Stripes Mocks
Stripes provides a rich set of classes that mock the different parts
of the Servlet API (HTTP request, response, and so on). Although we
can use each part individually, most of the time it's easier to use the
higher-level MockRoundtrip object and let it take care of managing the
request, response, and other underlying parts. Using MockRoundtrip
involves three steps:
1. Set up a MockServletContext object with parameters much like the
ones in the web.xml file. We need to do this only once for all the
tests that run within the same context.
7. I'll be using only basic JUnit code here. See Thomas and Hunt's Pragmatic Unit Testing
in Java with JUnit [ HT03 ] for more advanced JUnit techniques.
 
 
Search WWH ::




Custom Search