3.6. Using the Jetty Plug-in to Start a Web ApplicationFunctional testing is usually harder than unit testing because functional testing requires a complete execution environment. This requirement is particularly tough to satisfy when you are testing a web application, as you need to start your application in a Servlet container. This lab demonstrates how to use the Jetty plug-in to start the web application from Figure 3-1, in the Jetty Servlet container. We selected Jetty because it is a very fast container and a quality Jetty plug-in is available for Maven. Note: Jetty takes only a few milliseconds to start up. 3.6.1. How do I do that?As shown in Figure 3-1, there is an acceptance subproject from which you will execute your acceptance tests (otherwise know as functional tests). The web project that you used earlier generated the WAR that you wish to test functionally. You need to tell the Jetty plug-in to deploy this WAR. You do this by defining a dependency in the acceptance project's project.xml, and by tagging the dependency with the jetty.bundle property: <dependency> <groupId>mdn</groupId> <artifactId>qotd-web</artifactId> <version>${pom.currentVersion}</version> <properties> <jetty.bundle>true</jetty.bundle> </properties> <type>war</type> </dependency> Let's configure the Jetty plug-in so that Jetty runs on port 8081 (by default it runs on 8080). You achieve this by adding the following property in the project.properties file, in the acceptance/ directory: maven.jetty.port = 8081 Start Jetty by executing the jetty:run goal as follows: C:\dev\mavenbook\code\qotd\acceptance>maven jetty:run _ _ _ _ | \/ |_ _ _Apache_ _ _ _ _ | |\/| / _` \ V / -_) ' \ ~ intelligent projects ~ |_| |_\_ _,_|\_/\_ _ _|_||_| v. 1.0.2 build:start: jetty:prepare-filesystem: jetty:config: jetty:run: [java] 09:17:54.414 EVENT Checking Resource aliases [java] 09:17:54.714 EVENT Starting Jetty/4.2.17 [java] 09:17:54.764 WARN!! Delete existing temp dir C:\DOCUME~1\[...][/qotd-web,jar: file:/C:/Documents%20and%20Settings/[...]/.maven/repository/mdn/wars/qotd-web-1.0. war!/] [java] 09:17:54.884 EVENT Started WebApplicationContext[/qotd-web,jar:file:/C:/ Documents%20and%20Settings/[...]/.maven/repository/mdn/wars/qotd-web-1.0.war!/] [java] 09:17:54.924 EVENT jsp: init [java] 09:17:55.135 EVENT default: init [java] 09:17:55.135 EVENT invoker: init [java] 09:17:55.215 EVENT Started SocketListener on 0.0.0.0:8081 [java] 09:17:55.215 EVENT Started org.mortbay.jetty.Server@117a8bd There; you've used Maven to start a Servlet container, and your web application is now running. You can open a browser and go to http://localhost:8081/qotd-web/ to get the output of the QuoteServlet (see Figure 3-6). Figure 3-6. Output from QuoteServlet from the web subprojectTo stop the running container, simply press Ctrl-C in the Maven Console window. 3.6.2. What just happened?You just started Jetty, a Servlet container, and all you had to do was add a simple property to the dependency on qotd-web. Take note of the level of convenience Maven provides to your development cycle. Instead of starting your own Servlet container and copying a WAR file to some deployment directory, you simply ran maven jetty:run and a Servlet container started to execute your project. |