Working with Context Paths

Spring Cloud Contract 支持上下文路径。

Spring Cloud Contract supports context paths.

唯一需要完全支持上下文路径的更改是生成者端的切换。此外,自动生成的测试必须使用明确模式。消费者端不受影响。为使生成的测试通过,你必须使用显式模式。以下示例说明如何将测试模式设置为 EXPLICIT

The only change needed to fully support context paths is the switch on the producer side. Also, the autogenerated tests must use explicit mode. The consumer side remains untouched. In order for the generated test to pass, you must use explicit mode. The following example shows how to set the test mode to EXPLICIT:

Maven
<plugin>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-contract-maven-plugin</artifactId>
    <version>${spring-cloud-contract.version}</version>
    <extensions>true</extensions>
    <configuration>
        <testMode>EXPLICIT</testMode>
    </configuration>
</plugin>
Gradle
contracts {
		testMode = 'EXPLICIT'
}

如此一来,你生成的测试就不会使用 MockMvc。这意味着生成的是真实请求,而且你必须设置生成的测试的基本类以在真实套接字上运行。

That way, you generate a test that does not use MockMvc. It means that you generate real requests and you need to set up your generated test’s base class to work on a real socket.

考虑以下契约:

Consider the following contract:

Unresolved directive in context-paths.adoc - include::{verifier_root_path}/src/test/groovy/org/springframework/cloud/contract/verifier/builder/SingleTestGeneratorSpec.groovy[]

以下示例说明如何设置基本类和 RestAssured:

The following example shows how to set up a base class and RestAssured:

Unresolved directive in context-paths.adoc - include::{verifier_root_path}/src/test/groovy/org/springframework/cloud/contract/verifier/builder/SingleTestGeneratorSpec.groovy[]

如果你采用这种方式:

If you do it this way:

  • All of your requests in the autogenerated tests are sent to the real endpoint with your context path included (for example, /my-context-path/url).

  • Your contracts reflect that you have a context path. Your generated stubs also have that information (for example, in the stubs, you have to call /my-context-path/url).