Java Reference
In-Depth Information
Unit-testing servlets is that simple, as illustrated in figure 10.3 . Instantiate the servlet,
provide it with whatever mock objects it needs, invoke the proper do method, and check
the results. This example showed getContentAsString ; additional tests in this
chapter will illustrate two other convenient methods: getForwardedUrl and getRe-
directedUrl .Withtheseclassesandmethodsavailable,nodeploymenttoaservletcon-
tainer is required.
Figure 10.3. Servlet tests using Spring mocks. The Spring API provides mock classes for the request, response, and
session, and captures outputs, forwards, and redirected URLs.
So far, however, I haven't used Groovy at all. What does Groovy provide to make servlet
development and testing easier? I'll answer that in the next section.
Unit testing isn't always enough, though. I'd like to prove that my application classes work
in practice as well, so I want to do an integration test, too. That means I need a servlet con-
tainer, some way to deploy my web application, and a way to trigger requests types other
than simple GETs. That's the subject of the next section.
10.3.2. Integration testing with Gradle
Gradle is a build tool implemented in Groovy, which was discussed extensively in chapter
5 on build processes. Gradle uses Groovy builder syntax to specify repositories, library de-
pendencies, and build tasks. Executing a build using one of the normal plugins (like the
Groovy plugin used throughout this topic) downloads any needed dependencies, compiles
and tests the code, and prepares a final report of the results. One of the advantages of work-
 
Search WWH ::




Custom Search