Introduce @AutoConfigureContractVerifierMessaging
as a new autoconfig switch for test cases (instead of relying on plain autoconfiguration). Fixes gh-33
This commit is contained in:
@@ -26,6 +26,7 @@ import org.springframework.cloud.contract.spec.Contract
|
||||
import org.springframework.cloud.contract.verifier.messaging.ContractVerifierMessage
|
||||
import org.springframework.cloud.contract.verifier.messaging.ContractVerifierMessaging
|
||||
import org.springframework.cloud.contract.verifier.messaging.ContractVerifierObjectMapper
|
||||
import org.springframework.cloud.contract.verifier.messaging.boot.AutoConfigureContractVerifierMessaging;
|
||||
import org.springframework.test.annotation.DirtiesContext
|
||||
import org.springframework.test.context.ContextConfiguration
|
||||
|
||||
@@ -41,6 +42,7 @@ import com.toomuchcoding.jsonassert.JsonAssertion
|
||||
// Context configuration would end up in base class
|
||||
@ContextConfiguration(classes = [CamelMessagingApplication], loader = SpringBootContextLoader)
|
||||
@DirtiesContext
|
||||
@AutoConfigureContractVerifierMessaging
|
||||
public class CamelMessagingApplicationSpec extends Specification {
|
||||
|
||||
// ALL CASES
|
||||
|
||||
@@ -24,6 +24,7 @@ import org.springframework.cloud.contract.spec.Contract
|
||||
import org.springframework.cloud.contract.verifier.messaging.ContractVerifierMessage
|
||||
import org.springframework.cloud.contract.verifier.messaging.ContractVerifierMessaging
|
||||
import org.springframework.cloud.contract.verifier.messaging.ContractVerifierObjectMapper
|
||||
import org.springframework.cloud.contract.verifier.messaging.boot.AutoConfigureContractVerifierMessaging;
|
||||
import org.springframework.test.annotation.DirtiesContext
|
||||
import org.springframework.test.context.ContextConfiguration
|
||||
|
||||
@@ -35,6 +36,7 @@ import com.toomuchcoding.jsonassert.JsonAssertion
|
||||
// Context configuration would end up in base class
|
||||
@ContextConfiguration(classes = [IntegrationMessagingApplication], loader = SpringBootContextLoader)
|
||||
@DirtiesContext
|
||||
@AutoConfigureContractVerifierMessaging
|
||||
public class IntegrationMessagingApplicationSpec extends Specification {
|
||||
|
||||
// ALL CASES
|
||||
|
||||
@@ -25,6 +25,7 @@ import org.springframework.cloud.contract.spec.Contract
|
||||
import org.springframework.cloud.contract.verifier.messaging.ContractVerifierMessage
|
||||
import org.springframework.cloud.contract.verifier.messaging.ContractVerifierMessaging
|
||||
import org.springframework.cloud.contract.verifier.messaging.ContractVerifierObjectMapper
|
||||
import org.springframework.cloud.contract.verifier.messaging.boot.AutoConfigureContractVerifierMessaging;
|
||||
import org.springframework.test.annotation.DirtiesContext
|
||||
import org.springframework.test.context.ContextConfiguration
|
||||
|
||||
@@ -40,6 +41,7 @@ import com.toomuchcoding.jsonassert.JsonAssertion
|
||||
// Context configuration would end up in base class
|
||||
@ContextConfiguration(classes = [SpringMessagingApplication], loader = SpringBootContextLoader)
|
||||
@DirtiesContext
|
||||
@AutoConfigureContractVerifierMessaging
|
||||
public class SpringApplicationSpec extends Specification {
|
||||
|
||||
// ALL CASES
|
||||
|
||||
@@ -24,6 +24,7 @@ import org.springframework.cloud.contract.spec.Contract
|
||||
import org.springframework.cloud.contract.verifier.messaging.ContractVerifierMessage
|
||||
import org.springframework.cloud.contract.verifier.messaging.ContractVerifierMessaging
|
||||
import org.springframework.cloud.contract.verifier.messaging.ContractVerifierObjectMapper
|
||||
import org.springframework.cloud.contract.verifier.messaging.boot.AutoConfigureContractVerifierMessaging;
|
||||
import org.springframework.test.annotation.DirtiesContext
|
||||
import org.springframework.test.context.ContextConfiguration
|
||||
|
||||
@@ -38,6 +39,7 @@ import com.toomuchcoding.jsonassert.JsonAssertion
|
||||
// Context configuration would end up in base class
|
||||
@ContextConfiguration(classes = [StreamMessagingApplication], loader = SpringBootContextLoader)
|
||||
@DirtiesContext
|
||||
@AutoConfigureContractVerifierMessaging
|
||||
public class StreamMessagingApplicationSpec extends Specification {
|
||||
|
||||
// ALL CASES
|
||||
|
||||
@@ -2,13 +2,11 @@ package com.example.sink;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.example.sink.ContractVerifierSampleStreamSinkApplication;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = ContractVerifierSampleStreamSinkApplication.class)
|
||||
@SpringBootTest
|
||||
public class ContractVerifierSampleStreamSinkApplicationTests {
|
||||
|
||||
@Test
|
||||
|
||||
@@ -29,7 +29,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
* @author Marius Bogoevici
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(classes = ContractVerifierSampleStreamSinkApplication.class, webEnvironment = WebEnvironment.NONE, properties = "spring.cloud.stream.bindings.input.destination=sensor-data")
|
||||
@SpringBootTest(webEnvironment = WebEnvironment.NONE, properties = "spring.cloud.stream.bindings.input.destination=sensor-data")
|
||||
@AutoConfigureStubRunner
|
||||
public class MessageConsumedTests {
|
||||
|
||||
|
||||
@@ -2,13 +2,11 @@ package com.example.source;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.example.source.ContractVerifierSampleStreamSourceApplication;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = ContractVerifierSampleStreamSourceApplication.class)
|
||||
@SpringBootTest
|
||||
public class ContractVerifierSampleStreamSourceApplicationTests {
|
||||
|
||||
@Test
|
||||
|
||||
@@ -18,12 +18,9 @@ package com.example.source;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.IntegrationTest;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.example.source.ContractVerifierSampleStreamSourceApplication;
|
||||
|
||||
/**
|
||||
* Base class for sensor autogenerated tests (used by Spring Cloud Contract).
|
||||
*
|
||||
@@ -32,8 +29,7 @@ import com.example.source.ContractVerifierSampleStreamSourceApplication;
|
||||
* @author Marius Bogoevici
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = ContractVerifierSampleStreamSourceApplication.class)
|
||||
@IntegrationTest("spring.cloud.stream.bindings.output.destination=sensor-data")
|
||||
@SpringBootTest(classes = ContractVerifierSampleStreamSourceApplication.class, properties="spring.cloud.stream.bindings.output.destination=sensor-data")
|
||||
public abstract class SensorSourceTestBase {
|
||||
|
||||
@Autowired
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
# Auto Configuration
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
org.springframework.cloud.contract.verifier.messaging.boot.AutoConfigureContractVerifierMessaging=\
|
||||
org.springframework.cloud.contract.verifier.messaging.camel.ContractVerifierCamelConfiguration
|
||||
|
||||
@@ -20,17 +20,24 @@ import org.springframework.cloud.contract.verifier.messaging.ContractVerifierMes
|
||||
import org.springframework.cloud.contract.verifier.messaging.ContractVerifierMessaging;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author Marcin Grzejszczak
|
||||
*/
|
||||
@Configuration
|
||||
public class ContractVerifierIntegrationConfiguration {
|
||||
|
||||
@Bean ContractVerifierMessaging contractVerifierMessaging(ApplicationContext applicationContext, ContractVerifierMessageBuilder contractVerifierMessageBuilder) {
|
||||
return new ContractVerifierIntegrationMessaging(applicationContext, contractVerifierMessageBuilder);
|
||||
@Bean
|
||||
public ContractVerifierMessaging<?, ?> contractVerifierMessaging(
|
||||
ApplicationContext applicationContext,
|
||||
ContractVerifierMessageBuilder<?,?> contractVerifierMessageBuilder) {
|
||||
return new ContractVerifierIntegrationMessaging<>(applicationContext,
|
||||
contractVerifierMessageBuilder);
|
||||
}
|
||||
|
||||
@Bean ContractVerifierMessageBuilder contractVerifierMessageBuilder() {
|
||||
return new ContractVerifierIntegrationMessageBuilder();
|
||||
@Bean
|
||||
public ContractVerifierMessageBuilder<?, ?> contractVerifierMessageBuilder() {
|
||||
return new ContractVerifierIntegrationMessageBuilder<>();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
# Auto Configuration
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
org.springframework.cloud.contract.verifier.messaging.boot.AutoConfigureContractVerifierMessaging=\
|
||||
org.springframework.cloud.contract.verifier.messaging.integration.ContractVerifierIntegrationConfiguration
|
||||
|
||||
@@ -21,5 +21,9 @@
|
||||
<groupId>javax.inject</groupId>
|
||||
<artifactId>javax.inject</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-test-autoconfigure</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright 2012-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.contract.verifier.messaging.boot;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@ImportAutoConfiguration
|
||||
public @interface AutoConfigureContractVerifierMessaging {
|
||||
|
||||
}
|
||||
@@ -21,6 +21,7 @@ import org.springframework.cloud.contract.verifier.messaging.ContractVerifierMes
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.messaging.Message;
|
||||
|
||||
/**
|
||||
* @author Marcin Grzejszczak
|
||||
@@ -28,11 +29,16 @@ import org.springframework.context.annotation.Configuration;
|
||||
@Configuration
|
||||
public class ContractVerifierStreamAutoConfiguration {
|
||||
|
||||
@Bean ContractVerifierMessaging contractVerifierMessaging(ApplicationContext applicationContext, ContractVerifierMessageBuilder contractVerifierMessageBuilder) {
|
||||
return new ContractVerifierStreamMessaging(applicationContext, contractVerifierMessageBuilder);
|
||||
@Bean
|
||||
ContractVerifierMessaging<?, ?> contractVerifierMessaging(
|
||||
ApplicationContext applicationContext,
|
||||
ContractVerifierMessageBuilder<Object, Message<Object>> contractVerifierMessageBuilder) {
|
||||
return new ContractVerifierStreamMessaging<Object>(applicationContext,
|
||||
contractVerifierMessageBuilder);
|
||||
}
|
||||
|
||||
@Bean ContractVerifierMessageBuilder contractVerifierMessageBuilder() {
|
||||
return new ContractVerifierStreamMessageBuilder();
|
||||
@Bean
|
||||
ContractVerifierMessageBuilder<?, ?> contractVerifierMessageBuilder() {
|
||||
return new ContractVerifierStreamMessageBuilder<>();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,12 +16,15 @@
|
||||
|
||||
package org.springframework.cloud.contract.verifier.messaging.stream;
|
||||
|
||||
import org.springframework.cloud.contract.verifier.messaging.ContractVerifierMessage;
|
||||
import org.springframework.cloud.contract.verifier.messaging.ContractVerifierMessageBuilder;
|
||||
import org.springframework.cloud.contract.verifier.messaging.ContractVerifierMessaging;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.contract.verifier.messaging.ContractVerifierMessage;
|
||||
import org.springframework.cloud.contract.verifier.messaging.ContractVerifierMessageBuilder;
|
||||
import org.springframework.cloud.contract.verifier.messaging.ContractVerifierMessaging;
|
||||
import org.springframework.cloud.stream.config.BindingProperties;
|
||||
import org.springframework.cloud.stream.config.ChannelBindingServiceProperties;
|
||||
import org.springframework.cloud.stream.test.binder.MessageCollector;
|
||||
@@ -29,9 +32,6 @@ import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @author Marcin Grzejszczak
|
||||
*/
|
||||
@@ -42,18 +42,16 @@ public class ContractVerifierStreamMessaging<T> implements
|
||||
|
||||
private final ApplicationContext context;
|
||||
private final MessageCollector messageCollector;
|
||||
private final ContractVerifierMessageBuilder builder;
|
||||
private final ContractVerifierMessageBuilder<T, Message<T>> builder;
|
||||
|
||||
@Autowired
|
||||
@SuppressWarnings("unchecked")
|
||||
public ContractVerifierStreamMessaging(ApplicationContext context, ContractVerifierMessageBuilder builder) {
|
||||
public ContractVerifierStreamMessaging(ApplicationContext context, ContractVerifierMessageBuilder<T, Message<T>> builder) {
|
||||
this.context = context;
|
||||
this.messageCollector = context.getBean(MessageCollector.class);
|
||||
this.builder = builder;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public void send(T payload, Map<String, Object> headers, String destination) {
|
||||
send(builder.create(payload, headers), destination);
|
||||
}
|
||||
@@ -75,7 +73,7 @@ public class ContractVerifierStreamMessaging<T> implements
|
||||
public ContractVerifierMessage<T, Message<T>> receiveMessage(String destination, long timeout, TimeUnit timeUnit) {
|
||||
try {
|
||||
MessageChannel messageChannel = context.getBean(resolvedDestination(destination), MessageChannel.class);
|
||||
return builder.create(messageCollector.forChannel(messageChannel).poll(timeout, timeUnit));
|
||||
return builder.create((Message<T>) messageCollector.forChannel(messageChannel).poll(timeout, timeUnit));
|
||||
} catch (Exception e) {
|
||||
log.error("Exception occurred while trying to read a message from " +
|
||||
" a channel with name [" + destination + "]", e);
|
||||
@@ -102,13 +100,11 @@ public class ContractVerifierStreamMessaging<T> implements
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public ContractVerifierMessage<T, Message<T>> create(T t, Map<String, Object> headers) {
|
||||
return builder.create(t, headers);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public ContractVerifierMessage<T, Message<T>> create(Message<T> message) {
|
||||
return builder.create(message);
|
||||
}
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
# Auto Configuration
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
org.springframework.cloud.contract.verifier.messaging.boot.AutoConfigureContractVerifierMessaging=\
|
||||
org.springframework.cloud.contract.verifier.messaging.stream.ContractVerifierStreamAutoConfiguration
|
||||
|
||||
Reference in New Issue
Block a user