Common Problem: I am testing a web application in a large service oriented architecture company. External services often fail in our test environment due to background noise. This prevents the integration tests from running for our service, since our service will not work if calls to these external services do not work. For this reason, we want us to be able to brush off external services, so that we do not have to depend on them, and we can isolate our own services.
There is a tool for this Mockey that we hope to use. This is a Java program that passes through the Jetty Embedded Server and acts as a proxy for service calls. Our web application is reconfigured to call Mockey instead of external services. Then, Mockey is configured to provide dynamically repulsive answers to these calls depending on the URL and header data that is being transmitted.
To use this tool, we would like to launch Mockey in the pre-integration phase of the Maven test cycle so that it is available for use in the integration phase.
The specific problem . To start and close Mockey during the pre-integration and testing phases after integration in the Maven life cycle, I wrote a Maven 3 plugin called mockey-maven -plugin:
File pom.xml mockey-maven-plugin:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.mockey</groupId>
<artifactId>mockey-maven-plugin</artifactId>
<packaging>maven-plugin</packaging>
<version>1.3</version>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.2.5</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.mycompany.mockey</groupId>
<artifactId>Mockey</artifactId>
<version>1.16.2015</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.4</version>
</plugin>
</plugins>
</build>
</project>
Hockey-maven-plugin StartMockey Class:
@Mojo(name="start-mockey")
@Execute(phase= LifecyclePhase.PACKAGE)
public class StartMockey extends AbstractMojo
{
@Parameter(property="mockey.skipStartup", defaultValue="false", required=true)
private Boolean skipStartup;
public Boolean getSkipStartup()
{
return skipStartup;
}
public void setSkipStartup(Boolean skipStartup)
{
this.skipStartup = skipStartup;
}
public void execute()
{
if(skipStartup)
{
getLog().info("Skipping Mockey startup");
return;
}
getLog().info("Starting Mockey");
List<String> argsList = new ArrayList<>();
String[] args = new String[argsList.size()];
argsList.toArray(args);
try
{
JettyRunner.main(args);
}
catch(Exception e)
{
getLog().error("Mockey died... :(");
}
getLog().info("mockey-maven-plugin now exiting");
}
}
The mockey-maven-plugin ShutdownMockey Class:
@Mojo(name="shutdown-mockey")
public class ShutdownMockey extends AbstractMojo
{
@Parameter(property="mockey.skipShutdown")
private Boolean skipShutdown;
public Boolean getSkipShutdown()
{
return skipShutdown;
}
public void setSkipShutdown(Boolean skipShutdown)
{
this.skipShutdown = skipShutdown;
}
public void execute()
{
if(skipShutdown)
{
getLog().info("Skipping Mockey shutdown");
return;
}
getLog().info("Shutting down Mockey");
JettyRunner.stopServer();
getLog().info("mockey-maven-plugin now exiting");
}
}
Input for plugin for mockey-maven-plugin in my pom.xml project file:
<plugin>
<groupId>com.mycompany.mockey</groupId>
<artifactId>mockey-maven-plugin</artifactId>
<version>1.3</version>
<configuration>
<skipShutdown>${keepMockeyRunning}</skipShutdown>
<skipStartup>${skipMockey}</skipStartup>
</configuration>
<executions>
<execution>
<id>start-mockey</id>
<goals>
<goal>start-mockey</goal>
</goals>
<phase>pre-integration-test</phase>
</execution>
<execution>
<id>shutdown-mockey</id>
<goals>
<goal>shutdown-mockey</goal>
</goals>
<phase>post-integration-test</phase>
</execution>
</executions>
</plugin>
This plugin is great for launching Mockey during the pre-integration phase, but blocks the build until the Mockey game exits. I'm not sure why this is happening, as I specifically added this annotation to prevent this problem:
@Execute(phase= LifecyclePhase.PACKAGE)
, , ( maven-tomcat7-plugin - ). , , .
:
- Maven build .
- () , .
- , , - mockey-maven-plugin -.
- - ( ) - . Mockey Jetty, JettyRunner.main(args) ( Mockey).
- (IE: ).
- , , - mockey-maven-plugin - shutdown-mockey.
- shutdown-mockey JettyRunner.stopServer(), JettyRunner Jetty. (, , , ), Jetty .
- Jetty, , , .
- Maven.
, :
- Maven build .
- () , .
- , , - mockey-maven-plugin -.
- - . Maven , .
- , .
- , .
- , . - ( ). JettyRunner.main(args), Mockey Jetty.
- , Jetty ( Maven).
, Maven, , , , . forking , , . Unix, . . , , , . . , , "" Maven, , ? . , , "forking" Maven:
:
- Maven , ?
- Maven , , ? , ?
- , Tomcat7 ( )?
- Tomcat7, - ?
(. ):
- , , ?