Java Reference
In-Depth Information
It's a simple groovlet, but it should still be tested. Integration-testing of web applications is
discussed later in this chapter, but the syntax in the next listing uses the same mechanism
for transmitting a GET request (use the Groovy JDK to convert a string to a URL and then
call URL's getText method) that was used in several earlier chapters.
Listing 10.5. HelloGroovletTest , an integration test for the hello groovlet
class HelloGroovletTest {
int port = 8163
@Test
void testHelloGroovletWithNoName() {
String response =
"http://localhost:$port /HelloGroovlet/hello.groovy"
. toURL (). text
assert 'Hello, World!' == response.trim()
}
@Test
void testHelloGroovletWithName() {
String response =
"http://localhost:$port /HelloGroovlet/hello.groovy?name=Dolly"
. toURL (). text
assert 'Hello, Dolly!' == response.trim()
}
}
There's nothing particularly surprising or unusual about this test, which is simple because
the groovlet only responds to GET requests.
Unit tests are also doable, based on the fact that the GroovyServlet is executing the
groovlet as a script with predefined variables. For example, the next listing shows a unit
test for the groovlet that uses an instance of the GroovyShell class and the Binding
class in a manner similar to that described in chapter 6 on testing.
Search WWH ::




Custom Search