Using Jetty Server with Junit test

You may also like to read:

In this post I will show you how to use Jetty server with Junit test case and how to deploy web application using Java program in Junit test.

If you already have an idea on how to create a maven project in Eclipse will be great otherwise I will tell you here how to create a maven project in Eclipse.

The following configurations are required in order to run the application

Eclipse Kepler
JDK 1.7
Tomcat 7
Have maven installed and configured
Jetty and Junit dependencies in pom.xml

Now we will see the below steps how to create a maven based spring project in Eclipse

First we will create service project

Step 1. Create a maven based web project in Eclipse

Go to File -> New -> Other. On popup window under Maven select Maven Project. Then click on Next. Select the workspace location – either default or browse the location. Click on Next. Now in next window select the row as highlighted from the below list of archtypes and click on Next button.


Now enter the required fields (Group Id, Artifact Id) as shown below

Group Id : com.roytuts
Artifact Id : webapp

Step 2. Modify the pom.xml file as shown below.

<project xmlns="" xmlns:xsi=""
    <name>webapp Maven Webapp</name>


        <!-- JETTY -->


Step 3. If you see JRE System Library[J2SE-1.4] then change the version by below process

Do right-click on the project and go to Build -> Configure build path, under Libraries tab click on JRE System Library[J2SE-1.4], click on Edit button and select the appropriate jdk 1.7 from the next window. Click on Finish then Ok.

Step 4. Modify web.xml file as below

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="" xmlns:xsi=""


Step 5. Create Junit test class for testing with Jetty server

package com.roytuts.webapp.test;


import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.webapp.WebAppContext;

public class WebappTest {

  private Server server;

  public void setUp() throws Exception {
    server = new Server(8080);
    WebAppContext webAppContext = new WebAppContext();

  public void tearDown() throws Exception {

  public void testWebappDeploy() {
    HttpURLConnection connection = null;
    try {
      URL url = new URL("http://localhost:8080/webapp/");
      connection = (HttpURLConnection) url.openConnection();
      if (connection.getResponseCode() != 200) {
        throw new RuntimeException("Failed! HTTP Error Code: " + connection.getResponseCode());
      BufferedReader br = new BufferedReader(new InputStreamReader((connection.getInputStream())));
      String str;
      while ((str = br.readLine()) != null) {
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    } finally {
      if (connection != null) {


Step 6. Run the above Junit test class, you will see the below output in the console

2016-01-19 11:30:24.886:INFO::Logging to STDERR via org.mortbay.log.StdErrLog
2016-01-19 11:30:24.933:INFO::jetty-6.1.26
2016-01-19 11:30:25.370:INFO::Started SocketConnector@
2016-01-19 11:30:26.681:INFO::Stopped SocketConnector@
<h2>Hello World!</h2>
2016-01-19 11:30:26.806:INFO::Shutdown hook executing
2016-01-19 11:30:26.806:INFO::Shutdown hook complete

Thanks for reading.

Using Jetty Server with Junit test

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top