SpringCloud 將運行器添加到項目

2023-12-12 17:55 更新

您可以在類路徑上同時使用Spring AMQP和Spring Cloud Contract Stub Runner,并設(shè)置屬性stubrunner.amqp.enabled=true。請記住用@AutoConfigureStubRunner注釋測試類。

 如果您已經(jīng)在類路徑上具有Stream and Integration,則需要通過設(shè)置stubrunner.stream.enabled=falsestubrunner.integration.enabled=false屬性來顯式禁用它們。

假設(shè)您具有以下Maven存儲庫,其中包含spring-cloud-contract-amqp-test應(yīng)用程序的已部署存根。

└── .m2
    └── repository
        └── com
            └── example
                └── spring-cloud-contract-amqp-test
                    ├── 0.4.0-SNAPSHOT
                    │   ├── spring-cloud-contract-amqp-test-0.4.0-SNAPSHOT.pom
                    │   ├── spring-cloud-contract-amqp-test-0.4.0-SNAPSHOT-stubs.jar
                    │   └── maven-metadata-local.xml
                    └── maven-metadata-local.xml

進一步假設(shè)存根包含以下結(jié)構(gòu):

├── META-INF
│   └── MANIFEST.MF
└── contracts
    └── shouldProduceValidPersonData.groovy

考慮以下合同:

Contract.make {
	// Human readable description
	description 'Should produce valid person data'
	// Label by means of which the output message can be triggered
	label 'contract-test.person.created.event'
	// input to the contract
	input {
		// the contract will be triggered by a method
		triggeredBy('createPerson()')
	}
	// output message of the contract
	outputMessage {
		// destination to which the output message will be sent
		sentTo 'contract-test.exchange'
		headers {
			header('contentType': 'application/json')
			header('__TypeId__': 'org.springframework.cloud.contract.stubrunner.messaging.amqp.Person')
		}
		// the body of the output message
		body([
				id  : $(consumer(9), producer(regex("[0-9]+"))),
				name: "me"
		])
	}
}

現(xiàn)在考慮以下Spring配置:

stubrunner:
  repositoryRoot: classpath:m2repo/repository/
  ids: org.springframework.cloud.contract.verifier.stubs.amqp:spring-cloud-contract-amqp-test:0.4.0-SNAPSHOT:stubs
  stubs-mode: remote
  amqp:
    enabled: true
server:
  port: 0

觸發(fā)消息

要使用上述合同觸發(fā)消息,請使用StubTrigger界面,如下所示:

stubTrigger.trigger("contract-test.person.created.event")

該消息的目的地為contract-test.exchange,因此Spring AMQP存根運行器集成查找與該交換有關(guān)的綁定。

@Bean
public Binding binding() {
	return BindingBuilder.bind(new Queue("test.queue"))
			.to(new DirectExchange("contract-test.exchange")).with("#");
}

綁定定義綁定隊列test.queue。結(jié)果,以下偵聽器定義將與合同消息匹配并調(diào)用。

@Bean
public SimpleMessageListenerContainer simpleMessageListenerContainer(
		ConnectionFactory connectionFactory,
		MessageListenerAdapter listenerAdapter) {
	SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
	container.setConnectionFactory(connectionFactory);
	container.setQueueNames("test.queue");
	container.setMessageListener(listenerAdapter);

	return container;
}

此外,以下帶注釋的偵聽器將匹配并被調(diào)用:

@RabbitListener(bindings = @QueueBinding(value = @Queue("test.queue"), exchange = @Exchange(value = "contract-test.exchange", ignoreDeclarationExceptions = "true")))
public void handlePerson(Person person) {
	this.person = person;
}
該消息被直接移交給與匹配的SimpleMessageListenerContainer相關(guān)聯(lián)的MessageListeneronMessage方法。

Spring AMQP測試配置

為了避免Spring AMQP在我們的測試期間嘗試連接到正在運行的代理,請配置模擬ConnectionFactory。

要禁用模擬的ConnectionFactory,請設(shè)置以下屬性:stubrunner.amqp.mockConnection=false

stubrunner:
  amqp:
    mockConnection: false


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號