对于最新的稳定版本,请使用 spring-cloud-contract 5.0.0!spring-doc.cadn.net.cn

在Artifactory中为非Spring应用提供提供商合同与存根的测试

在本页中,你将学习如何使用非 Spring 应用程序和上传到 Artifactory 的存根进行提供商合同测试。spring-doc.cadn.net.cn

流动

你可以阅读《开发你的第一个春云基于合同应用》,了解在Nexus或Artifactory中使用存根进行提供商合同测试的流程。spring-doc.cadn.net.cn

建立消费者

对于消费者端,你可以用JUnit规则。这样你就不必开始春季语境。以下列表展示了此类规则(见JUnit4和JUnit 5);spring-doc.cadn.net.cn

JUnit 4 规则
@Rule
	public StubRunnerRule rule = new StubRunnerRule()
			.downloadStub("com.example","artifact-id", "0.0.1")
			.repoRoot("git://[email protected]:spring-cloud-samples/spring-cloud-contract-nodejs-contracts-git.git")
			.stubsMode(StubRunnerProperties.StubsMode.REMOTE);
JUnit 5 扩展
@RegisterExtension
	public StubRunnerExtension stubRunnerExtension = new StubRunnerExtension()
			.downloadStub("com.example","artifact-id", "0.0.1")
			.repoRoot("git://[email protected]:spring-cloud-samples/spring-cloud-contract-nodejs-contracts-git.git")
			.stubsMode(StubRunnerProperties.StubsMode.REMOTE);

制作人设立

默认情况下,Spring Cloud Contract 插件使用 Rest Assurance 的莫克麦克为 生成测试。由于非 Spring 应用不使用莫克麦克,你可以更改测试模式明确发送真实请求给绑定在特定端口的应用程序。spring-doc.cadn.net.cn

在这个例子中,我们使用一个叫 Javalin 的框架来启动一个 非 Spring HTTP 服务器。spring-doc.cadn.net.cn

假设我们有以下应用:spring-doc.cadn.net.cn

import io.javalin.Javalin;

public class DemoApplication {

	public static void main(String[] args) {
		new DemoApplication().run(7000);
	}

	public Javalin start(int port) {
		return Javalin.create().start(port);
	}

	public Javalin registerGet(Javalin app) {
		return app.get("/", ctx -> ctx.result("Hello World"));
	}

	public Javalin run(int port) {
		return registerGet(start(port));
	}

}

给定该应用程序,我们可以设置插件使用明确模式(即 到 向真实端口发送请求,具体如下:spring-doc.cadn.net.cn

梅文
<plugin>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-contract-maven-plugin</artifactId>
	<version>${spring-cloud-contract.version}</version>
	<extensions>true</extensions>
	<configuration>
		<baseClassForTests>com.example.demo.BaseClass</baseClassForTests>
		<!-- This will setup the EXPLICIT mode for the tests -->
		<testMode>EXPLICIT</testMode>
	</configuration>
</plugin>
格拉德勒
contracts {
	// This will setup the EXPLICIT mode for the tests
	testMode = "EXPLICIT"
	baseClassForTests = "com.example.demo.BaseClass"
}

基类可能类似于以下内容:spring-doc.cadn.net.cn

import io.javalin.Javalin;
import io.restassured.RestAssured;
import org.junit.After;
import org.junit.Before;
import org.springframework.cloud.test.TestSocketUtils;

public class BaseClass {

	Javalin app;

	@Before
	public void setup() {
		// pick a random port
		int port = TestSocketUtils.findAvailableTcpPort();
		// start the application at a random port
		this.app = start(port);
		// tell Rest Assured where the started application is
		RestAssured.baseURI = "http://localhost:" + port;
	}

	@After
	public void close() {
		// stop the server after each test
		this.app.stop();
	}

	private Javalin start(int port) {
		// reuse the production logic to start a server
		return new DemoApplication().run(port);
	}
}

采用这样的设置:spring-doc.cadn.net.cn