消息传递

Spring Cloud Contract 允许你验证使用消息传递作为通信手段的应用。本文档中显示的所有集成都与 Spring 配合使用,但你也可以创建自己的集成并使用。

消息传递 DSL 顶层元素

消息传递的 DSL 与侧重于 HTTP 的 DSL 略有不同。以下各节解释了这些差异。

由方法触发的输出

输出消息可以通过调用一个方法(例如契约启动时以及消息发送时的 Scheduler)触发,如下例所示。

Groovy
def dsl = Contract.make {
	// Human readable description
	description 'Some description'
	// Label by means of which the output message can be triggered
	label 'some_label'
	// input to the contract
	input {
		// the contract will be triggered by a method
		triggeredBy('bookReturnedTriggered()')
	}
	// output message of the contract
	outputMessage {
		// destination to which the output message will be sent
		sentTo('output')
		// the body of the output message
		body('''{ "bookName" : "foo" }''')
		// the headers of the output message
		headers {
			header('BOOK-NAME', 'foo')
		}
	}
}
YAML
# Human readable description
description: Some description
# Label by means of which the output message can be triggered
label: some_label
input:
  # the contract will be triggered by a method
  triggeredBy: bookReturnedTriggered()
# output message of the contract
outputMessage:
  # destination to which the output message will be sent
  sentTo: output
  # the body of the output message
  body:
    bookName: foo
  # the headers of the output message
  headers:
    BOOK-NAME: foo

在前面的示例中,如果调用了名为 bookReturnedTriggered 的方法,则输出消息会发送到 output。在消息发布者端,我们生成一个测试,该测试调用该方法来触发消息。在消费者端,你可以使用 some_label 来触发消息。

消费者/生产者

本节仅对 Groovy DSL 有效。

在 HTTP 中,你有 client/stubserver/test 的概念。你也可以在消息传递中使用这些范例。此外,Spring Cloud Contract Verifier 还提供了 consumerproducer 方法(请注意,你可以使用 $value 方法来提供 consumerproducer 部分)。

通用

inputoutputMessage 部分中,你可以调用 assertThat,传入你在基类或静态导入中定义的 method 名称(例如,assertThatMessageIsOnTheQueue())。Spring Cloud Contract 会在生成的测试中运行该方法。

集成

你可以使用以下集成配置之一

  • Apache Camel

  • Spring Integration

  • Spring Cloud Stream

  • Spring JMS

由于我们使用了 Spring Boot,如果你已将其中一个库添加到类路径中,所有消息传递配置都会自动设置。

记得在生成的测试的基类上添加 @AutoConfigureMessageVerifier 注解。否则,Spring Cloud Contract 的消息传递部分将无法工作。

如果你想使用 Spring Cloud Stream,请记得添加对 org.springframework.cloud:spring-cloud-stream 的测试依赖,如下所示

Maven
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-stream</artifactId>
    <type>test-jar</type>
    <scope>test</scope>
    <classifier>test-binder</classifier>
</dependency>
Gradle
testImplementation(group: 'org.springframework.cloud', name: 'spring-cloud-stream', classifier: 'test-binder')

手动集成测试

测试使用的主要接口是 org.springframework.cloud.contract.verifier.messaging.MessageVerifierSenderorg.springframework.cloud.contract.verifier.messaging.MessageVerifierReceiver。它们定义了如何发送和接收消息。

在测试中,你可以注入 ContractVerifierMessageExchange 来发送和接收符合契约的消息。然后将 @AutoConfigureMessageVerifier 添加到你的测试中。以下示例展示了如何做到这一点。

@RunWith(SpringTestRunner.class)
@SpringBootTest
@AutoConfigureMessageVerifier
public static class MessagingContractTests {

  @Autowired
  private MessageVerifier verifier;
  ...
}
如果你的测试也需要存根,那么 @AutoConfigureStubRunner 就包含了消息传递配置,所以你只需要这一个注解即可。

生产者端消息测试生成

在 DSL 中包含 inputoutputMessage 部分会在发布者端生成测试。默认情况下,会创建 JUnit 4 测试。但是,也可以创建 JUnit 5、TestNG 或 Spock 测试。

传递给 messageFromsentTo 的目标对于不同的消息传递实现可能有不同的含义。对于 Stream 和 Integration,它首先被解析为通道的 destination。然后,如果不存在这样的 destination,则解析为通道名称。对于 Camel,它是一个特定的组件(例如 jms)。

考虑以下契约

Groovy
def contractDsl = Contract.make {
	name "foo"
	label 'some_label'
	input {
		triggeredBy('bookReturnedTriggered()')
	}
	outputMessage {
		sentTo('activemq:output')
		body('''{ "bookName" : "foo" }''')
		headers {
			header('BOOK-NAME', 'foo')
			messagingContentType(applicationJson())
		}
	}
}
YAML
label: some_label
input:
  triggeredBy: bookReturnedTriggered
outputMessage:
  sentTo: activemq:output
  body:
    bookName: foo
  headers:
    BOOK-NAME: foo
    contentType: application/json

对于前面的示例,将创建以下测试

JUnit
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import org.junit.Test;
import org.junit.Rule;
import javax.inject.Inject;
import org.springframework.cloud.contract.verifier.messaging.internal.ContractVerifierObjectMapper;
import org.springframework.cloud.contract.verifier.messaging.internal.ContractVerifierMessage;
import org.springframework.cloud.contract.verifier.messaging.internal.ContractVerifierMessaging;

import static org.springframework.cloud.contract.verifier.assertion.SpringCloudContractAssertions.assertThat;
import static org.springframework.cloud.contract.verifier.util.ContractVerifierUtil.*;
import static com.toomuchcoding.jsonassert.JsonAssertion.assertThatJson;
import static org.springframework.cloud.contract.verifier.messaging.util.ContractVerifierMessagingUtil.headers;
import static org.springframework.cloud.contract.verifier.util.ContractVerifierUtil.fileToBytes;

public class FooTest {
	@Inject ContractVerifierMessaging contractVerifierMessaging;
	@Inject ContractVerifierObjectMapper contractVerifierObjectMapper;

	@Test
	public void validate_foo() throws Exception {
		// when:
			bookReturnedTriggered();

		// then:
			ContractVerifierMessage response = contractVerifierMessaging.receive("activemq:output",
					contract(this, "foo.yml"));
			assertThat(response).isNotNull();

		// and:
			assertThat(response.getHeader("BOOK-NAME")).isNotNull();
			assertThat(response.getHeader("BOOK-NAME").toString()).isEqualTo("foo");
			assertThat(response.getHeader("contentType")).isNotNull();
			assertThat(response.getHeader("contentType").toString()).isEqualTo("application/json");

		// and:
			DocumentContext parsedJson = JsonPath.parse(contractVerifierObjectMapper.writeValueAsString(response.getPayload()));
			assertThatJson(parsedJson).field("['bookName']").isEqualTo("foo");
	}

}
Spock
import com.jayway.jsonpath.DocumentContext
import com.jayway.jsonpath.JsonPath
import spock.lang.Specification
import javax.inject.Inject
import org.springframework.cloud.contract.verifier.messaging.internal.ContractVerifierObjectMapper
import org.springframework.cloud.contract.verifier.messaging.internal.ContractVerifierMessage
import org.springframework.cloud.contract.verifier.messaging.internal.ContractVerifierMessaging

import static org.springframework.cloud.contract.verifier.assertion.SpringCloudContractAssertions.assertThat
import static org.springframework.cloud.contract.verifier.util.ContractVerifierUtil.*
import static com.toomuchcoding.jsonassert.JsonAssertion.assertThatJson
import static org.springframework.cloud.contract.verifier.messaging.util.ContractVerifierMessagingUtil.headers
import static org.springframework.cloud.contract.verifier.util.ContractVerifierUtil.fileToBytes

class FooSpec extends Specification {
	@Inject ContractVerifierMessaging contractVerifierMessaging
	@Inject ContractVerifierObjectMapper contractVerifierObjectMapper

	def validate_foo() throws Exception {
		when:
			bookReturnedTriggered()

		then:
			ContractVerifierMessage response = contractVerifierMessaging.receive("activemq:output",
					contract(this, "foo.yml"))
			response != null

		and:
			response.getHeader("BOOK-NAME") != null
			response.getHeader("BOOK-NAME").toString() == 'foo'
			response.getHeader("contentType") != null
			response.getHeader("contentType").toString() == 'application/json'

		and:
			DocumentContext parsedJson = JsonPath.parse(contractVerifierObjectMapper.writeValueAsString(response.getPayload()))
			assertThatJson(parsedJson).field("['bookName']").isEqualTo("foo")
	}

}

消费者存根生成

与 HTTP 部分不同,在消息传递中,我们需要将契约定义与存根一起发布到 JAR 中。然后,它在消费者端被解析,并创建相应的存根路由。

如果类路径中有多个框架,Stub Runner 需要定义应该使用哪个框架。假设你的类路径中有 AMQP、Spring Cloud Stream 和 Spring Integration,并且你想使用 Spring AMQP。那么你需要设置 stubrunner.stream.enabled=falsestubrunner.integration.enabled=false。这样,唯一剩下的框架就是 Spring AMQP。

存根触发

要触发消息,请使用 StubTrigger 接口,如下例所示

import java.util.Collection;
import java.util.Map;

/**
 * Contract for triggering stub messages.
 *
 * @author Marcin Grzejszczak
 */
public interface StubTrigger {

	/**
	 * Triggers an event by a given label for a given {@code groupid:artifactid} notation.
	 * You can use only {@code artifactId} too.
	 *
	 * Feature related to messaging.
	 * @param ivyNotation ivy notation of a stub
	 * @param labelName name of the label to trigger
	 * @return true - if managed to run a trigger
	 */
	boolean trigger(String ivyNotation, String labelName);

	/**
	 * Triggers an event by a given label.
	 *
	 * Feature related to messaging.
	 * @param labelName name of the label to trigger
	 * @return true - if managed to run a trigger
	 */
	boolean trigger(String labelName);

	/**
	 * Triggers all possible events.
	 *
	 * Feature related to messaging.
	 * @return true - if managed to run a trigger
	 */
	boolean trigger();

	/**
	 * Feature related to messaging.
	 * @return a mapping of ivy notation of a dependency to all the labels it has.
	 */
	Map<String, Collection<String>> labels();

}

为了方便起见,StubFinder 接口扩展了 StubTrigger,因此在你的测试中只需要其中一个即可。

StubTrigger 提供了以下选项来触发消息

按标签触发

以下示例展示了如何使用标签触发消息

stubFinder.trigger('return_book_1')

按 Group ID 和 Artifact ID 触发

以下示例展示了如何按 Group ID 和 Artifact ID 触发消息

stubFinder.trigger('org.springframework.cloud.contract.verifier.stubs:streamService', 'return_book_1')

按 Artifact ID 触发

以下示例展示了如何从 Artifact ID 触发消息

stubFinder.trigger('streamService', 'return_book_1')

触发所有消息

以下示例展示了如何触发所有消息

stubFinder.trigger()

消费者端消息传递与 Apache Camel

Spring Cloud Contract Stub Runner 的消息传递模块提供了与 Apache Camel 集成的简便方法。对于提供的 artifacts,它会自动下载存根并注册所需的路由。

将 Apache Camel 添加到项目

你可以在类路径中同时包含 Apache Camel 和 Spring Cloud Contract Stub Runner。记得使用 @AutoConfigureStubRunner 注解标记你的测试类。

禁用此功能

如果你需要禁用此功能,请设置 stubrunner.camel.enabled=false 属性。

示例

假设我们有以下 Maven 仓库,其中部署了 camelService 应用的存根

└── .m2
    └── repository
        └── io
            └── codearte
                └── accurest
                    └── stubs
                        └── camelService
                            ├── 0.0.1-SNAPSHOT
                            │   ├── camelService-0.0.1-SNAPSHOT.pom
                            │   ├── camelService-0.0.1-SNAPSHOT-stubs.jar
                            │   └── maven-metadata-local.xml
                            └── maven-metadata-local.xml

此外,假设存根包含以下结构

├── META-INF
│   └── MANIFEST.MF
└── repository
    ├── accurest
    │   └── bookReturned1.groovy
    └── mappings

现在考虑以下契约

Contract.make {
	label 'return_book_1'
	input {
		triggeredBy('bookReturnedTriggered()')
	}
	outputMessage {
		sentTo('rabbitmq:output?queue=output')
		body('''{ "bookName" : "foo" }''')
		headers {
			header('BOOK-NAME', 'foo')
		}
	}
}

要从 return_book_1 标签触发消息,我们使用 StubTrigger 接口,如下所示

stubFinder.trigger("return_book_1")

这将把消息发送到契约的输出消息中描述的目标。

消费者端消息传递与 Spring Integration

Spring Cloud Contract Stub Runner 的消息传递模块提供了与 Spring Integration 集成的简便方法。对于提供的 artifacts,它会自动下载存根并注册所需的路由。

将 Runner 添加到项目

你可以在类路径中同时包含 Spring Integration 和 Spring Cloud Contract Stub Runner。记得使用 @AutoConfigureStubRunner 注解标记你的测试类。

禁用此功能

如果你需要禁用此功能,请设置 stubrunner.integration.enabled=false 属性。

示例

假设你拥有以下 Maven 仓库,其中部署了 integrationService 应用的存根

└── .m2
    └── repository
        └── io
            └── codearte
                └── accurest
                    └── stubs
                        └── integrationService
                            ├── 0.0.1-SNAPSHOT
                            │   ├── integrationService-0.0.1-SNAPSHOT.pom
                            │   ├── integrationService-0.0.1-SNAPSHOT-stubs.jar
                            │   └── maven-metadata-local.xml
                            └── maven-metadata-local.xml

此外,假设存根包含以下结构

├── META-INF
│   └── MANIFEST.MF
└── repository
    ├── accurest
    │   └── bookReturned1.groovy
    └── mappings

考虑以下契约

Contract.make {
	label 'return_book_1'
	input {
		triggeredBy('bookReturnedTriggered()')
	}
	outputMessage {
		sentTo('output')
		body('''{ "bookName" : "foo" }''')
		headers {
			header('BOOK-NAME', 'foo')
		}
	}
}

现在考虑以下 Spring Integration 路由

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
			 xmlns:beans="http://www.springframework.org/schema/beans"
			 xmlns="http://www.springframework.org/schema/integration"
			 xsi:schemaLocation="http://www.springframework.org/schema/beans
			https://www.springframework.org/schema/beans/spring-beans.xsd
			http://www.springframework.org/schema/integration
			http://www.springframework.org/schema/integration/spring-integration.xsd">


	<!-- REQUIRED FOR TESTING -->
	<bridge input-channel="output"
			output-channel="outputTest"/>

	<channel id="outputTest">
		<queue/>
	</channel>

</beans:beans>

要从 return_book_1 标签触发消息,请使用 StubTrigger 接口,如下所示

stubFinder.trigger('return_book_1')

这将把消息发送到契约的输出消息中描述的目标。

消费者端消息传递与 Spring Cloud Stream

Spring Cloud Contract Stub Runner 的消息传递模块提供了与 Spring Stream 集成的简便方法。对于提供的 artifacts,它会自动下载存根并注册所需的路由。

如果 Stub Runner 与 Stream 集成时,messageFromsentTo 字符串首先被解析为通道的 destination,并且不存在这样的 destination,则该目标将被解析为通道名称。

如果你想使用 Spring Cloud Stream,请记得添加对 org.springframework.cloud:spring-cloud-stream 测试支持的依赖,如下所示

Maven
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-stream-test-binder</artifactId>
    <scope>test</scope>
</dependency>
Gradle
testImplementation('org.springframework.cloud:spring-cloud-stream-test-binder')

将 Runner 添加到项目

你可以在类路径中同时包含 Spring Cloud Stream 和 Spring Cloud Contract Stub Runner。记得使用 @AutoConfigureStubRunner 注解标记你的测试类。

禁用此功能

如果你需要禁用此功能,请设置 stubrunner.stream.enabled=false 属性。

示例

假设你拥有以下 Maven 仓库,其中部署了 streamService 应用的存根

└── .m2
    └── repository
        └── io
            └── codearte
                └── accurest
                    └── stubs
                        └── streamService
                            ├── 0.0.1-SNAPSHOT
                            │   ├── streamService-0.0.1-SNAPSHOT.pom
                            │   ├── streamService-0.0.1-SNAPSHOT-stubs.jar
                            │   └── maven-metadata-local.xml
                            └── maven-metadata-local.xml

此外,假设存根包含以下结构

├── META-INF
│   └── MANIFEST.MF
└── repository
    ├── accurest
    │   └── bookReturned1.groovy
    └── mappings

考虑以下契约

Contract.make {
	label 'return_book_1'
	input { triggeredBy('bookReturnedTriggered()') }
	outputMessage {
		sentTo('returnBook')
		body('''{ "bookName" : "foo" }''')
		headers { header('BOOK-NAME', 'foo') }
	}
}

现在考虑以下 Spring Cloud Stream 函数配置

@ImportAutoConfiguration(TestChannelBinderConfiguration.class)
@Configuration(proxyBeanMethods = true)
@EnableAutoConfiguration
protected static class Config {

	@Bean
	Function<String, String> test1() {
		return (input) -> {
			println "Test 1 [${input}]"
			return input
		}
	}

}

现在考虑以下 Spring 配置

stubrunner.repositoryRoot: classpath:m2repo/repository/
stubrunner.ids: org.springframework.cloud.contract.verifier.stubs:streamService:0.0.1-SNAPSHOT:stubs
stubrunner.stubs-mode: remote
spring:
  cloud:
    stream:
      bindings:
        test1-in-0:
          destination: returnBook
        test1-out-0:
          destination: outputToAssertBook
    function:
      definition: test1

server:
  port: 0

debug: true

要从 return_book_1 标签触发消息,请使用 StubTrigger 接口,如下所示

stubFinder.trigger('return_book_1')

这将把消息发送到契约的输出消息中描述的目标。

消费者端消息传递与 Spring JMS

Spring Cloud Contract Stub Runner 的消息传递模块提供了与 Spring JMS 集成的简便方法。

此集成假设你有一个正在运行的 JMS 消息代理实例。

将 Runner 添加到项目

你的类路径中需要同时包含 Spring JMS 和 Spring Cloud Contract Stub Runner。记得使用 @AutoConfigureStubRunner 注解标记你的测试类。

示例

假设存根结构如下所示

├── stubs
    └── bookReturned1.groovy

此外,假设以下测试配置

stubrunner:
  repository-root: stubs:classpath:/stubs/
  ids: my:stubs
  stubs-mode: remote
spring:
  activemq:
    send-timeout: 1000
  jms:
    template:
      receive-timeout: 1000

现在考虑以下契约

Contract.make {
	label 'return_book_1'
	input {
		triggeredBy('bookReturnedTriggered()')
	}
	outputMessage {
		sentTo('output')
		body('''{ "bookName" : "foo" }''')
		headers {
			header('BOOKNAME', 'foo')
		}
	}
}

要从 return_book_1 标签触发消息,我们使用 StubTrigger 接口,如下所示

stubFinder.trigger('return_book_1')

这将把消息发送到契约的输出消息中描述的目标。