同一模块可同时做契约与存根测试吗?构建报错求助
Absolutely, you can run both contract tests and stub tests in the same module—but you need to tweak your Maven plugin configuration to fix the timing issue you're hitting. Let's break down what's going wrong and how to fix it.
Why You're Seeing That Error
The MetadataNotFoundException and the late execution of spring-cloud-contract-maven-plugin:2.0.0.RC1:generateStubs boil down to one core problem: your stubs are being generated after your tests (or dependency resolution step) try to use them. By default, the generateStubs goal is bound to the package phase of the Maven lifecycle, which runs after the test phase—so when your tests look for the my-service:stubs artifact, it doesn't exist yet.
Fixes to Get Both Tests Working in the Same Module
Here are actionable steps to resolve the timing issue:
1. Move the generateStubs Phase Earlier
Bind the generateStubs goal to a Maven phase that runs before your tests execute. The process-test-resources or test-compile phase works well here, ensuring stubs are ready when tests start.
Update your pom.xml plugin configuration:
<plugin> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-contract-maven-plugin</artifactId> <version>2.0.0.RC1</version> <executions> <execution> <id>default-generateStubs</id> <!-- Run stubs generation before tests compile --> <phase>test-compile</phase> <goals> <goal>generateStubs</goal> </goals> </execution> </executions> </plugin>
2. Install Stubs to Local Maven Repo Before Testing
If your tests directly depend on the stubs artifact (e.g., via a test-scoped dependency), you need to ensure it's installed to your local repo before tests run. Add the maven-install-plugin to install the generated stubs early:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-install-plugin</artifactId> <version>3.1.0</version> <executions> <execution> <id>install-stubs</id> <phase>test-compile</phase> <goals> <goal>install-file</goal> </goals> <configuration> <file>${project.build.directory}/stubs/${project.artifactId}-${project.version}-stubs.jar</file> <groupId>${project.groupId}</groupId> <artifactId>${project.artifactId}</artifactId> <version>${project.version}</version> <classifier>stubs</classifier> <packaging>jar</packaging> </configuration> </execution> </executions> </plugin>
3. Use Local Stubs Directly in Tests
For even more reliability, configure your stub tests to read directly from the local stubs directory (instead of pulling from Maven repo). If you're using Spring Cloud Contract Stub Runner, set the stubsMode to LOCAL:
In your test class:
@SpringBootTest @AutoConfigureStubRunner( stubsMode = StubRunnerProperties.StubsMode.LOCAL, ids = "example.com:my-service:+:stubs:8080" ) public class MyConsumerStubTest { // Your test logic here }
Or add this to application-test.properties:
stubrunner.stubs-mode=LOCAL stubrunner.ids=example.com:my-service:+:stubs:8080
Wrap-Up
With these tweaks, your stubs will be generated early enough for both contract tests (producer-side validation) and stub tests (consumer-side simulation) to use them in the same module. The metadata resolution error should disappear once the stubs exist before the dependency check runs.
内容的提问来源于stack exchange,提问作者Doron Levi

