I have a maven project for a client library. I want to have my Maven build test that library against a real API using jUnit tests, and I want to stand the API up as part of my Maven build. I don't want the API to be on the classpath of my jUnit tests because my application uses Spring Framework's component scan and so additional things on the classpath could skew my results.
Is there a way that Maven could start my API with its own dependencies/classpath, run my jUnit tests with the test classpath of my module/project, then stop the API?
First Option I considered
Initially I thought I could just start the API from my jUnit tests, but the problem is that then I would need to include the API code and all its baggage in my test classpath, and I have a feeling this could skew my test results by bringing in dependencies and configuration that the library consumers won't necessarily have.
Second Option I considered
I also looked at using the spring boot plugin to start/stop the API using the test classpath on the pre- and post-integration phases, having the API as a "test"-scoped dependency, and using the failsafe plugin with some dependency exclusions to run my test methods using the library. However, this seems like a really complex way to do it and I'm having some trouble getting it working (namely, the start/stop executions can't seem to find the main class I specify for the API even though I configured it to use the test classpath). Here is what I have so far for that. Both the project with these plugins, and the clienttarget
dependency are modules within another project.
<dependencies>
<dependency>
<groupId>com.example.test</groupId>
<artifactId>clienttarget</artifactId>
<version>3.0.LOCAL.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.example.testapi.ClientTargetApiApplication</mainClass>
<useTestClasspath>true</useTestClasspath>
</configuration>
<executions>
<execution>
<id>pre-integration-test</id>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>post-integration-test</id>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<classpathDependencyExcludes>
<classpathDependencyExclude>com.example.test:clienttarget</classpathDependencyExclude>
</classpathDependencyExcludes>
<excludedGroups>com.example.test.IntegrationTest</excludedGroups>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<classpathDependencyExcludes>com.example.test:clienttarget</classpathDependencyExcludes>
<groups>com.example.test.IntegrationTest</groups>
</configuration>
</plugin>
</plugins>
Update: I've found a similar question, but it is not concerned with the classpath specifically, but instead with only having separate processes running at the same time: End to end integration test for multiple spring boot applications under Maven