Fork me on GitHub

Test Tutorial

This tutorial looks at testing a WoOF application.

To focus on testing, the below simple application to add two numbers will be used.

TestHttpServer screen shot.

Download Tutorial Source

Application code

The application being tested has one page.

<html>
	<body>
		<form action="#{add}">
			<p><input type="text" name="a" value="${a}" /> 
			+ <input type="text" name="b" value="${b}" /> 
			= ${result}</p>
			<input type="submit" value="add" />
		</form>
	</body>
</html>

With the backing logic class.

public class TemplateLogic {

	@Data
	@HttpParameters
	public static class Parameters implements Serializable {

		private String a;

		private String b;

		private String result;
	}

	public Parameters getTemplateData(Parameters parameters) {
		return parameters;
	}

	public void add(Parameters parameters) {
		
		int a = Integer.parseInt(parameters.getA());
		int b = Integer.parseInt(parameters.getB());

		parameters.setResult(String.valueOf(a + b));
	}

}

The focus of the tutorial is to test that the two numbers are added correctly. Production quality testing would cover a lot more test scenarios for the application. The additional scenarios have been excluded for brevity as they would follow a similar pattern as described by this tutorial.

Unit Testing

The following demonstrates unit testing the template logic class. As the template logic class is a POJO (Plain Old Java Object), it is unit tested like any other Java class.

	@Test
	public void unitTest() {

		// Load the parameters
		Parameters parameters = new Parameters();
		parameters.setA("1");
		parameters.setB("2");
		assertNull("Shoud not have result", parameters.getResult());

		// Test
		TemplateLogic logic = new TemplateLogic();
		logic.add(parameters);
		assertEquals("Incorrect result", "3", parameters.getResult());
	}

This is a simple application. More complex applications may be dependency injecting more complex objects such as a javax.sql.DataSource or javax.persistence.EntityManager. Again as the template logic is a POJO, any mocking framework can be used to mock these objects for use in testing.

System Testing

As OfficeFloor is a very light weight framework, it starts and stops fast enough to be included in the setup and teardown of unit tests. This allows for system testing the application as follows. Note that the starting and stopping of WoOF (OfficeFloor) can be included in setup and teardown methods to not clutter individual tests.

	@Test
	public void systemTest() throws Exception {

		// Start the application
		WoofOfficeFloorSource.start();

		try (CloseableHttpClient client = HttpTestUtil.createHttpClient()) {

			// Send request to add
			HttpGet request = new HttpGet(
					"http://localhost:7878/template-add.woof?a=1&b=2");
			HttpResponse response = client.execute(request);

			// Ensure added the values
			String entity = EntityUtils.toString(response.getEntity());
			assertTrue("Should have added the values", entity.contains("= 3"));

		} finally {
			// Stop the application
			WoofOfficeFloorSource.stop();
		}
	}

Integration Testing

OfficeFloor provides Maven plug-ins to enable integration testing. The following is the pom.xml plug-ins starting/stopping WoOF and running the integration tests.

			<plugin>
				<!-- Start/Stop application for Integration tests -->
				<groupId>net.officefloor.maven</groupId>
				<artifactId>woof-maven-plugin</artifactId>
				<executions>
					<execution>
						<id>run-WoOF</id>
						<phase>pre-integration-test</phase>
						<goals>
							<goal>run</goal>
						</goals>
					</execution>
					<execution>
						<id>stop-WoOF</id>
						<phase>post-integration-test</phase>
						<goals>
							<goal>stop</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<!-- Run Integration tests -->
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-failsafe-plugin</artifactId>
				<executions>
					<execution>
						<goals>
							<goal>integration-test</goal>
							<goal>verify</goal>
						</goals>
					</execution>
				</executions>
			</plugin>

The integration test is then as follows.

	@Test
	public void integrationTest() throws Exception {

		try (CloseableHttpClient client = HttpTestUtil.createHttpClient()) {

			// Send request to add
			HttpGet request = new HttpGet(
					"http://localhost:7878/template-add.woof?a=1&b=2");
			HttpResponse response = client.execute(request);

			// Ensure added the values
			String entity = EntityUtils.toString(response.getEntity());
			assertTrue("Should have added the values", entity.contains("= 3"));
		}
	}

Note that the above is a simple request test. As the WoOF maven plugin runs the application with the same code that deploys it to run anywhere (e.g. Cloud Computing), this enables using more complex integration test tools (e.g. SeleniumHQ) to validate the application before deploying it.

Next

Please see the other tutorials for further features.