4

我对两个消费者和一个生产者进行了测试,离线工作正常,但是当我更改它们以从 Artifactory 检索存根时,消费者测试失败。

这是离线工作的代码:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ContractTestConfiguration.class, webEnvironment = SpringBootTest.WebEnvironment.NONE)
@AutoConfigureStubRunner(ids = {"com.mycompany:service-name:+:stubs"}, workOffline = true)
@ImportAutoConfiguration(org.springframework.cloud.stream.test.binder.TestSupportBinderAutoConfiguration.class)
@DirtiesContext
public class MyContractTest

这是在线的:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ContractTestConfiguration.class, webEnvironment = SpringBootTest.WebEnvironment.NONE)
@AutoConfigureStubRunner(ids = {"com.mycompany:service-name:+:stubs"}, repositoryRoot = "https://artifactory.companyname.com/artifactory/artifacts-snapshot-local")
@ImportAutoConfiguration(org.springframework.cloud.stream.test.binder.TestSupportBinderAutoConfiguration.class)
@DirtiesContext
public class MyContractTest {

我收到此错误:

Exception occurred while trying to download a stub for group [com.mycompany] module [service-name] and classifier [stubs] in [remote0 (https://artifactory.mycompany.com/artifactory/artifacts-snapshot-local, default, releases+snapshots)]
org.eclipse.aether.resolution.ArtifactResolutionException: Could not find artifact com.mycompany.domain:service-name:jar:stubs:1.6.0-SNAPSHOT

我查看了 Artifactory 和https://artifactory.mycompany.com/artifactory/artifacts-snapshot-local,存根 jar 出现在那里。我已经完成了生产者的 mvn 安装,当我再次运行测试时,我收到此错误“工件已在本地存储库中找到,但您已明确声明它应该从远程存储库下载”。

我还尝试向消费者添加对生产者存根的依赖,但我得到了类似的错误。我宁愿避免它,因为它会添加与特定版本的生产者的依赖关系:

<dependency>
    <groupId>com.companyname</groupId>
    <artifactId>service-name</artifactId>
    <classifier>stubs</classifier>
    <version>1.6.0-SNAPSHOT</version>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>*</groupId>
            <artifactId>*</artifactId>
        </exclusion>
    </exclusions>
</dependency>

我已将此添加到生产者的 POM 文件中:

<spring.cloud.contract.verifier.skip>true</spring.cloud.contract.verifier.skip>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
        <execution>
            <id>stub</id>
            <goals>
                <goal>single</goal>
            </goals>
            <phase>prepare-package</phase>
            <inherited>false</inherited>
            <configuration>
                <attach>true</attach>
                <descriptor>${basedir}/src/assembly/stub.xml</descriptor>
            </configuration>
        </execution>
    </executions>
</plugin>

这是 src/assembly 下的文件 stub.xml 的内容:

<assembly
        xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
    <id>stubs</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>src/main/java</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>**com/companyname/projectname/*.*</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${project.build.directory}/classes</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>**com/companyname/projectname/*.*</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${project.build.directory}/snippets/stubs</directory>
            <outputDirectory>META-INF/${project.groupId}/${project.artifactId}/${project.version}/mappings</outputDirectory>
            <includes>
                <include>**/*</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${basedir}/src/test/resources/contracts</directory>
            <outputDirectory>META-INF/${project.groupId}/${project.artifactId}/${project.version}/contracts</outputDirectory>
            <includes>
                <include>**/*.groovy</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

知道我缺少什么吗?提前致谢

4

2 回答 2

3

我查看了 Artifactory 和https://artifactory.mycompany.com/artifactory/artifacts-snapshot-local,存根 jar 出现在那里。我已经完成了生产者的 mvn 安装,当我再次运行测试时,我收到此错误“工件已在本地存储库中找到,但您已明确声明它应该从远程存储库下载”。

当您在本地安装存根时会发生这种情况,然后尝试从工件下载它,但 SHA 不同,因此 Aether(下载存根的引擎)选择本地的。在这种情况下,我们会抛出一个异常,因为您想从远程位置下载存根,而不是从本地下载存根。

尝试在 [remote0 ( https://artifactory.mycompany.com/artifactory/artifacts-snapshot-local,默认值,版本+快照)] org.eclipse.aether.resolution.ArtifactResolutionException: 找不到工件 com.mycompany.domain:service-name:jar:stubs:1.6.0-SNAPSHOT

这看起来像在 Artifactory 中,您在一些 Maven 元数据中输入了最新的 jar,1.6.0-SNAPSHOT但 JAR 不再存在。你能仔细检查它是否真的存在吗?

我还尝试向消费者添加对生产者存根的依赖,但我得到了类似的错误。我宁愿避免它,因为它会添加与特定版本的生产者的依赖关系:

这只能证明您的工件/项目设置有问题。如果您对版本进行硬编码,事情仍然无法正常工作吗?

更新:

如果您的工件实例需要凭据或位于代理后面,您可以使用这些值:

https://github.com/spring-cloud/spring-cloud-contract/blob/v1.1.4.RELEASE/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/ stubrunner/spring/StubRunnerProperties.java#L72-L87

您可以提供stubrunner.usernamestubrunner.passwordstubrunner.proxyHoststubrunner.proxyPort

于 2017-11-27T15:16:34.837 回答
0

可以在注释stubrunner.username中提供吗?我尝试了以下方法:stubrunner.password@AutoConfigureStubRunner

 @RunWith(SpringRunner.class)
 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
 @AutoConfigureStubRunner(ids = "com.mycompany.myproj:myservice:+:stubs:8100",
    repositoryRoot = "http://artifactory.mycompany.com/artifactory/libs-snapshot-local",
    properties = {"stubrunner.username=myusername", "stubrunner.password=mypassword"},
    stubsMode = StubRunnerProperties.StubsMode.REMOTE)
 @DirtiesContext
 class MyApiContractsVerificationTest {
     ...
 }

凭据正确,存根已正确生成并部署到远程 Artifactory 存储库中。如果我将它们配置为在本地 .m2/repository 中查看(删除“repositoryRoot 和”properties”注释参数),测试运行良好,但使用上述配置我收到以下错误:

Could not find metadata com.mycommany.myproject:myservice/maven-metadata.xml in local (/Users/myname/.m2/repository), 
org.eclipse.aether.transfer.MetadataTransferException: Could not transfer metadata com.mycommany.myproject:myservice/maven-metadata.xml from/to remote0 (http://artifactory.mycompany.com/artifactory/libs-snapshot-local): **Unauthorized (401)**]
...

在启用远程模式运行测试之前,我确实清理了存根的本地 .m2/repository,因此没有冲突。

我是否错误地提供了用户名和密码?配置中是否存在不正确或缺失的内容?

于 2018-05-11T19:07:26.103 回答