<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Christian Schenk&#187; servlets</title>
	<atom:link href="http://www.christianschenk.org/blog/tag/servlets/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.christianschenk.org</link>
	<description>Writing about my experiences with technology and all different kinds of projects and experiments</description>
	<lastBuildDate>Sun, 04 Dec 2011 23:43:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Testing web applications with Jetty</title>
		<link>http://www.christianschenk.org/blog/testing-web-applications-with-jetty/</link>
		<comments>http://www.christianschenk.org/blog/testing-web-applications-with-jetty/#comments</comments>
		<pubDate>Wed, 25 Jul 2007 12:00:19 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jetty]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[servlets]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[webapps]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/blog/testing-web-applications-with-jetty/</guid>
		<description><![CDATA[Shows how to deploy and test your servlets with Jetty - everything inside a JUnit test]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;d like to test your web application you might be interested in <a title="Jetty - WebServer" href="http://www.mortbay.org/">Jetty</a>: you can deploy your servlets in it, generate HTTP requests and check if certain criteria are met &#8211; everything inside a JUnit test.</p>
<p>Jetty has got enough <a title="Jetty Documentation" href="http://docs.codehaus.org/display/JETTY/Jetty+Documentation">documentation</a> to get you started; if you want more you can get support from the <a title="WebTide" href="http://www.webtide.com/">creators</a> of Jetty. This post was inspired by <a title="Testing servlets with an embedded Jetty server" href="http://docs.codehaus.org/display/JETTY/ServletTester">this</a> particular page from the docs. I setup a very small Eclipse project that can be downloaded as <a href="http://data.christianschenk.org/testing-web-applications-with-jetty/TestingWithJetty.tar.gz">tar.gz</a> or <a href="http://data.christianschenk.org/testing-web-applications-with-jetty/TestingWithJetty.zip">zip</a>; alternatively, you can browse the code <a title="TestingWithJetty - Code Reference" href="http://data.christianschenk.org/testing-web-applications-with-jetty/xref/index.html">here</a>.</p>
<p><span id="more-31"></span></p>
<h2>JUnit scaffolding</h2>
<p>Lets have a look at a small JUnit class, that implements a <code>setUp</code> method to bootstrap Jetty:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.Before</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.mortbay.jetty.testing.HttpTester</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.mortbay.jetty.testing.ServletTester</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> WebAppTest <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">private</span> ServletTester tester<span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> HttpTester request<span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> HttpTester response<span style="color: #339933;">;</span>
&nbsp;
  @Before
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">tester</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ServletTester<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">tester</span>.<span style="color: #006633;">setContextPath</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">tester</span>.<span style="color: #006633;">addServlet</span><span style="color: #009900;">&#40;</span>MyServlet.<span style="color: #000000; font-weight: bold;">class</span>, <span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">tester</span>.<span style="color: #006633;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">request</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HttpTester<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">response</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HttpTester<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">request</span>.<span style="color: #006633;">setMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;GET&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">request</span>.<span style="color: #006633;">setHeader</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Host&quot;</span>, <span style="color: #0000ff;">&quot;tester&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">request</span>.<span style="color: #006633;">setVersion</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;HTTP/1.0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>This method leverages the features provided by the <code>ServletTester</code> class, which will create a Jetty server for us. Instead of writing a <code>web.xml</code>, we can add servlets to the server programmatically. After that we start the server and create a request/response pair that can be used in the test methods.</p>
<h2>Testing</h2>
<p>Now you can write several test methods for every request you&#8217;d like to check. If you want to check the start page of your web application you set the URI, generate a request, send it to Jetty, receive the response and check it like so:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">  @Test
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testHomepage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">request</span>.<span style="color: #006633;">setURI</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">response</span>.<span style="color: #006633;">parse</span><span style="color: #009900;">&#40;</span>tester.<span style="color: #006633;">getResponses</span><span style="color: #009900;">&#40;</span>request.<span style="color: #006633;">generate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    assertTrue<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">response</span>.<span style="color: #006633;">getMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">200</span>, <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">response</span>.<span style="color: #006633;">getStatus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello World&quot;</span>, <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">response</span>.<span style="color: #006633;">getContent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>In this simple case the <code>MyServlet</code> class contains a line like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">  response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello World&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Conclusion</h2>
<p>Jetty is said to be highly embeddable. As far as I&#8217;ve checked it out, that&#8217;s absolutely true. Due to this it&#8217;s very easy to use Jetty  inside unit tests. I haven&#8217;t tried to use Jetty with <a href="http://www.bibsonomy.org/user/cschenk/web+framework+software">web frameworks</a> but that should be no problem either. So if you&#8217;d like to test your servlets I recommend checking out <a title="Jetty - WebServer" href="http://www.mortbay.org/">Jetty</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/testing-web-applications-with-jetty/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

