Automate integration testing with Maven Failsafe plugin

Since it was found that the integration test can be automated by using Maven Failsafe Plugin, it is described as a memorandum.

For example, it can be used when you want to automate the following tests

--Send a request to the application that creates response information with Servlet and filter, and confirm that the expected response is returned.

Sample program

environment

Application implementation

--Create a Maven project with the Maven archetype below image.png

--Define dependencies in the form below

pom.xml


	<dependencies>
		<!-- Servlet API -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>4.0.1</version>
			<scope>provided</scope>
		</dependency>
		<!--client-->
		<dependency>
			<groupId>org.glassfish.jersey.core</groupId>
			<artifactId>jersey-client</artifactId>
			<version>2.28</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.glassfish.jersey.inject</groupId>
			<artifactId>jersey-hk2</artifactId>
			<version>2.28</version>
			<scope>test</scope>
		</dependency>
		<!-- JUnit -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
			<scope>test</scope>
		</dependency>
	</dependencies>

--Prepare the following source code

DemoServlet.java


import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/demo")
public class DemoServlet extends HttpServlet {

	private static final long serialVersionUID = 2564544155028547344L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.getWriter().println("body");
	}
}

DemoFilter.java


import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

@WebFilter("/demo")
public class DemoFilter implements Filter {

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpServletResponseWrapper wrapper = new HttpServletResponseWrapper((HttpServletResponse) response);
		wrapper.setHeader("Header-Name", "value");
		chain.doFilter(request, wrapper);
	}

	@Override
	public void destroy() {
	}
}

--Prepare the following test code

DemoIT.java


import static org.junit.Assert.assertEquals;

import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Response;

import org.junit.Test;

public class DemoIT {

	@Test
	public void test() {
		//Send request to server
		Response response = ClientBuilder.newClient().target("http://localhost:8080/sample-it/demo").request().get();
		//Make sure the filter is working as expected
		assertEquals("value", response.getHeaderString("Header-Name"));
		//Make sure the Servlet is working as expected
		assertEquals("body", response.readEntity(String.class).trim());
	}
}

Define Failsafe plugin

later,

--Start application server --Deploy the application to the server --Run test class --Stop the application server

Make the process automatically executed according to the flow of.

For that, use the Failsafe plugin. (The Failsafe plugin is a plugin that can control the test execution. You can define the pre-processing and post-processing of the test execution as described above.) Since you cannot start the server or deploy the application with the Failsafe plugin, Use Apache Tomcat Maven plugin. The following is set.

pom.xml


	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-failsafe-plugin</artifactId>
				<version>3.0.0-M3</version>
				<executions>
					<execution>
						<goals>
							<goal>integration-test</goal>
							<goal>verify</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<!--Plugin for operating Tomcat-->
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<version>2.2</version>
				<executions>
					<!--Test execution pre-processing-->
					<execution>
						<id>run</id>
						<phase>pre-integration-test</phase>
						<goals>
							<!--Start Tomcat / Deploy application-->
							<goal>run</goal>
						</goals>
						<!--Settings to enable execution of goals after run-->
						<configuration>
							<fork>true</fork>
						</configuration>
					</execution>
					<!--Post-test processing-->
					<execution>
						<id>shutdown</id>
						<phase>post-integration-test</phase>
						<goals>
							<!--Tomcat stopped-->
							<goal>shutdown</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

The test can be performed by executing the following command.

mvn clean verify

Spring Boot system may not be worth using Failsafe plugin because it seems that integration test can be automated by using SprintBootTest. However, for applications such as Spring MVC that must be deployed on a server, this plugin can be used to automate it. It seems to be usable when there are many specification changes such as agile.

Recommended Posts

Automate integration testing with Maven Failsafe plugin
Integration Test with Gradle
Testing model with RSpec
Plugin development with ImageJ
Automate format with maven-formatter
Combine system-scoped jar files into one with Maven Shade Plugin
Combine system-scoped jar files into one with Maven Dependency Plugin