Jetty Maven Plugin and REST Assured

The Jetty Maven plugin is useful for rapid development and testing. We can add it to any webapp project that is structured according to the Maven defaults. The plugin can then periodically scan your project for changes and automatically redeploy the webapp if any are found. This makes the development cycle more productive by eliminating the build and deploy steps: you use your IDE to make changes to the project, and the running web container automatically picks them up, allowing you to test them straight away.

For most of my case, I'm using it for integration testing, in combination it with maven-failsafe-plugin. For REST projects, I combined it with REST Assured.

Upgrading Jetty Maven plugin

Lately I'm upgrading my project from Spring 4 to Spring 5, and together I also upgrade Jetty Maven Plugin to latest version. Here the configuration in pom.xml before upgrade:

...

<properties>        
	...
	<!-- application server configuration -->
	<appserver.port.http>9699</appserver.port.http>
	<appserver.deployment.context>/</appserver.deployment.context>
	...
</properties>

...

<build>
	<plugins>
		...
		<plugin>
			<groupId>org.mortbay.jetty</groupId>
			<artifactId>maven-jetty-plugin</artifactId>
			<version>6.1.26</version>
			<configuration>
				<webAppConfig>
					<contextPath>${appserver.deployment.context}</contextPath>
				</webAppConfig>
				<connectors>
					<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
						<port>${appserver.port.http}</port>
					</connector>
				</connectors>
				<stopPort>10066</stopPort>
				<stopKey>jetty</stopKey>
			</configuration>				
			<executions>
				<execution>
					<id>start-jetty</id>
					<phase>pre-integration-test</phase>
					<goals>
						<goal>run</goal>
					</goals>
					<configuration>
						<scanIntervalSeconds>0</scanIntervalSeconds>
						<daemon>true</daemon>
					</configuration>
				</execution>
				<execution>
					<id>stop-jetty</id>
					<phase>post-integration-test</phase>
					<goals>
						<goal>stop</goal>
					</goals>
				</execution>
			</executions>
		</plugin>
		...
	</plugins>
</build>
...
                    

And after upgrade:

<plugin>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>9.4.20.v20190813</version>
    <configuration>
        <webAppConfig>
            <contextPath>${appserver.deployment.context}</contextPath>
        </webAppConfig>
        <httpConnector>
            <port>${appserver.port.http}</port>
        </httpConnector>
        <stopPort>10066</stopPort>
        <stopKey>jetty</stopKey>
    </configuration>				
    <executions>
        <execution>
            <id>start-jetty</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>start</goal>
            </goals>
            <configuration>
                <scanIntervalSeconds>0</scanIntervalSeconds>
                <daemon>true</daemon>
            </configuration>
        </execution>
        <execution>
            <id>stop-jetty</id>
            <phase>post-integration-test</phase>
            <goals>
                <goal>stop</goal>
            </goals>
        </execution>
    </executions>
</plugin>
                    

Version jump from 6.1.26 to 9.4.20.v20190813 is not an obstacle. Indeed configuration must change, ex: http port assigment and goal in start-jetty from run to start. Jetty itself change home several time, which on 2009, the core components of Jetty have been moved to Eclipse.org.

We should use Maven 3.3+ for this plugin. For more about jetty-maven-plugin, please refer to their official documentation.

Upgrading REST Assured

Testing and validation of REST services in Java is harder than in dynamic languages such as Ruby and Groovy. REST Assured brings the simplicity of using these languages into the Java domain.

The configuration in pom.xml before upgrade:

...
<rest-assured.version>2.8.0</rest-assured.version>
...
<dependency>
    <groupId>com.jayway.restassured</groupId>
    <artifactId>rest-assured</artifactId>
    <version>${rest-assured.version}</version>
    <scope>test</scope>
</dependency>
                    

And after upgrade:

...
<rest-assured.version>4.1.1</rest-assured.version>
...
<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured</artifactId>
    <version>${rest-assured.version}</version>
    <scope>test</scope>
</dependency>
                    

After upgrade, the package name need to change from com.jayway.restassured to io.restassured. As example, is in import statements below:

import static com.jayway.restassured.RestAssured.with;
import com.jayway.restassured.authentication.FormAuthConfig;
                    

Change to:

import static io.restassured.RestAssured.with;
import io.restassured.authentication.FormAuthConfig;
                    

The way we use FormAuthConfig also need to change. Previously this form accept full url like "http://localhost:9699/j_spring_security_check". Now we just need to specify j_spring_security_check.

You can refer to old project poc-swagger-springmvc which is part of my previous article POC: Swagger - Spring MVC (And Swagger UI) and Action Report: poc-swagger-springmvc in my old blog. And check the upgraded version in spring-rest-examples. Previous article Remove DefaultAnnotationHandlerMapping is wrote about upgrading to Spring 5.

See also: HTTP Methods in Spring RESTful Services