Sans Pareil Technologies, Inc.

Key To Your Business

Lab1 : Demo Web Application


We will develop a simple web application using the IntelliJ IDE and Gradle build system. The notes assume that IntelliJ community edition and JDK 1.8 have been installed on the computer.

Note that the example uses an embedded Jetty server to run the test suite also. If we had used only Servlet’s in our demo, we would not have needed to use an embedded servlet container, as HttpUnit comes with its own. This is just one of the reasons why JSP has been considered evil and obsolete since the early 2000’s.

Project

Create and set up the project dependencies.

  • Create a new IntelliJ project.
  • Select Java 1.8 as the SDK
  • Select Gradle as the project template on the left pane.
  • Select Java and Web options on the right pane.
  • Click the Next button to move to the next screen.
  • Use mis283 as the GroupId - The group id is used to identify the publisher of a library/application if you were publishing artefacts to a central repository such as maven central.
  • Use demo as the ArtefactId - The is the name of the artefact (war file, jar file etc) that will be assembled by Gradle.
  • Click the Next button to move to the next screen.
  • Select 1.8 JVM as the Gradle JVM.
  • Click the Next button to move to the next screen.
  • Specify demo as Project name.
  • Specify an appropriate location (eg. ~/Desktop/demo) as the Project location.
  • Click the Finish button.
  • Open the build.gradle file in the IDE. Edit the file and add the following additional lines to the appropriate sections (see build.gradle file for details):

apply from: 'https://raw.github.com/akhikhl/gretty/master/pluginScripts/gretty.plugin'
testCompile 'org.httpunit:httpunit:1.7.2' group 'mis283'

  • Run the project from the Gradle pane in the IDE. In the popup for the target, specify appRun. This will download (on first run) all required dependencies, build and bundle our demo app and deploy and run the app on an embedded Jetty servlet container. Once the server has started visit the demo application from your web browser at http://localhost:8080/demo/

build.gradle

Modify the IDE generated gradle build file to match the following:

group 'mis283'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'war'
apply from: 'https://raw.github.com/akhikhl/gretty/master/pluginScripts/gretty.plugin'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

gretty {
    integrationTestTask = 'test'
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    testCompile 'org.httpunit:httpunit:1.7.2'
}

index.jsp

Modify the index.jsp file under src/main/webapp directory. We will add some standard text to the JSP file, and later validate the text displayed from our test suite.

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>MIS 283 - Demo Application</title>
  </head>
  <body>
  <h1 id="greeting1">Hello World!</h1>
  <h2 id="greeting2">Moraine Valley Community College</h2>
  <h3 id="greeting3">MIS 283 - Java Web Applications</h3>
  <div id="message">
    This is a demo application that will show how to use HttpUnit to validate the text displayed on this JSP page.
  </div>
  </body>
</html>


Refresh the page in your browser to see the updated page.

Explore the build directory under your project to see the Java source that was generated for the JSP file.

Test class

We will create a test class that will test the contents displayed in our JSP page.
  • We will now add our test class that
  • Expand the src/test tree and right-click on the java directory and select the New->Package menu to create a new package. Name the package mis283.
  • Right-click the newly created package and select New->Java Class to create a new class. Name the class DemoTest.
  • Open the newly generated Java class file in the IDE editor, and add our test code (see below).
  • Run the test gradle target to run the test suite (in this case a single test class with a single test method).

package mis283;

import com.meterware.httpunit.HTMLElement;
import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class DemoTest {
    @Test
    public void index() throws Exception {
        final WebConversation wc = new WebConversation();
        final WebRequest request = new GetMethodWebRequest( "http://localhost:8080/demo/" );
        final WebResponse response = wc.getResponse( request );

        assertEquals( "MIS 283 - Demo Application", response.getTitle() );

        HTMLElement element = response.getElementWithID( "greeting1" );
        assertEquals( "Hello World!", element.getText() );

        element = response.getElementWithID( "greeting2" );
        assertEquals( "Moraine Valley Community College", element.getText() );

        element = response.getElementWithID( "greeting3" );
        assertEquals( "MIS 283 - Java Web Applications", element.getText() );

        element = response.getElementWithID( "message" );
        assertEquals(
                "This is a demo application that will show how to use HttpUnit to validate the text displayed on this JSP page.",
                element.getText() );
    }
}