【问题标题】:Spring Cloud Contract test cannot find stubs from classpath in IntellijSpring Cloud Contract 测试无法从 Intellij 中的类路径中找到存根
【发布时间】:2020-05-09 15:19:58
【问题描述】:

我使用 Spring Cloud Contract 为 HTTP 编写了一个非常简单的合约测试。我在 /src/test/resources/contracts 下创建了具有合同定义的生产者:

package contracts

import org.springframework.cloud.contract.spec.Contract

Contract.make {
    request {
        method 'GET'
        url '/documents/123456789'
        headers {
            contentType('application/json')
        }
    }
    response {
        status OK()
        body([
               id  : 123456789,
               status: "VALID"
        ])
        headers {
            contentType('application/json')
        }
    }
}

生产者是一个带有定义的maven模块:

<groupId>com.sample</groupId>
<artifactId>rest-producer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>rest-producer</name>

我只将此生产者部署到我的 Maven 本地存储库。之后,我创建了一个依赖于生产者存根的消费者:

<dependency>
    <groupId>com.sample</groupId>
    <artifactId>rest-producer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <classifier>stubs</classifier>
    <scope>test</scope>
</dependency>

我定义了一个简单的测试,它使用类路径来搜索存根:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
@AutoConfigureStubRunner(ids = "com.sample:rest-producer:+:stubs:8080")
class DocumentReaderIntTest {
...
}

当我通过全新安装使用 Maven 构建运行此测试时,一切正常。但是,当我尝试通过 Intellij 的 RunConfiguration 运行此测试时,我得到一个无法找到存根的异常。我应该如何设置才能使 Intellij 也能进行此测试?

编辑: 我发现当我只打开消费者项目然后在类路径上它有来自本地 maven repo 的 jar 时:

/Users/adam/.m2/repository/com/sample/rest-producer/0.0.1-SNAPSHOT/rest-producer-0.0.1-SNAPSHOT-stubs.jar

但是,当我在同一个项目中导入生产者时,它使用类路径中的 target/classes 文件夹,因此找不到合同定义:

/Users/adam/projects/rest-producer/target/classes

【问题讨论】:

    标签: spring-cloud spring-cloud-contract


    【解决方案1】:

    是的,这是 Intellij 的一个已知问题。当您在多模块项目中引用类路径依赖项时,Intellij 不会构建存根 jar,但会直接引用其他模块的代码。也许向 Intellij 提出问题会有意义?

    【讨论】:

      猜你喜欢
      • 2014-10-20
      • 1970-01-01
      • 1970-01-01
      • 2018-10-08
      • 1970-01-01
      • 1970-01-01
      • 2019-09-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多