IntroductionSome automatic tests, as integration tests, can require external resources. Junit provides a mechanism to simplify the management of these external resources in an automatic test context: Rules. The Petals JUnit Framework comes with several external resources ready to use:
|
Table of contents Contributors
No contributors found for: authors on selected page(s)
|
A Web server as external resource
The WebServer rule allows creation of files and folders that are guaranteed to be deleted when the test method finishes (whether it passes or fails):
public static class HasWebServer { @Rule public WebServer webServer = new WebServer(); @Test public void testUsingWebServer() throws IOException { // ... this.webServer.addServlet(servlet, servletPath); // ... } }
Creating the web-server
The creation of the web-server requires the HTTP port on which it will listen. The default listening port is '{{WebServer.DEFAULT_HTTP_PORT}'. You can use your own HTTP port using the right constructor:
// A web server listening on the default HTTP port @Rule public WebServer webServer = new WebServer(); // A web server listening on your own HTTP port @Rule public WebServer webServer = new WebServer(8080);
Registering servlet
A web-server maps URLs on servlets. If your test requires a servlet, you can register a servlet through the API 'WebServer.addServlet(httpServlet, path)', where 'httpServlet' is your servlet to map on the URL path part '{path}}':
public static class HasWebServer { @Rule public WebServer webServer = new WebServer(); @Test public void testUsingWebServer() throws IOException { // ... this.webServer.addServlet(servlet, servletPath); // ... } }
Pre-defined servlets
Several servlets are provided into the Petals JUnit Framework:
- FileServlet that replies with the content of a file,
- TextServlet that replies with a plain-text content.
Working with FileServlet
The reply of this servlet is:
- the content-type that has been set when creating the servlet,
- the reply content is the content of the given file when creating the servlet.
public static class HasWebServer { @Rule public WebServer webServer = new WebServer(); @Test public void testUsingWebServer() throws IOException { // ... final AbstractHttpServlet fileServlet = new FileServlet( new File(Thread.currentThread().getContextClassLoader().getResource("my-file").toURI()), MimeTypeConstants.APPLICATION_ZIP); this.webServer.addServlet(fileServlet, fileServlet.getPath()); // ... } }
Working with TextServlet
The reply of this servlet is:
- the content-type is 'text/plain',
- the reply content is 'A plain text'.
public static class HasWebServer { @Rule public WebServer webServer = new WebServer(); @Test public void testUsingWebServer() throws IOException { // ... final AbstractHttpServlet textServlet = new textServlet(); this.webServer.addServlet(fileServlet, fileServlet.getPath()); // ... } }
Getting the URL of a servlet to invoke
Some facilities was included to build the URL of a servlet to invoke:
- WebServer.getHttpBaseUrl() : returns the base part of URLs served by the web server,
- AbstractHttpServlet.getPath() : returns the URL path part of the servlet.
Combining both methods builds easily the URL of the servlet to invoke:public static class HasWebServer { @Rule public WebServer webServer = new WebServer(); @Test public void testUsingWebServer() throws IOException { // ... final AbstractHttpServlet textServlet = new textServlet(); this.webServer.addServlet(fileServlet, fileServlet.getPath()); // ... final URL urlToInvoke = new URL(this.webServer.getHttpBaseUrl() + textServlet.getPath()); // ... } }