如何在Spring Cloud Stream中使用useNativeEncoding = true测试绑定

时间:2019-06-20 10:13:46

标签: spring-kafka spring-cloud-stream

我正在尝试测试配置了以下绑定的应用程序:

spring:
  cloud:
    stream:
      bindings:
        accountSource:
          destination: account
          producer:
            useNativeEncoding: true
      kafka:
        binder:
          brokers: ${KAFKA_BOOTSTRAP_ADDRESSES}
          producer-properties:
            schema.registry.url: ${KAFKA_SCHEMA_REGISTRY_URL}
            value.subject.name.strategy: io.confluent.kafka.serializers.subject.RecordNameStrategy
        bindings:
          accountSource:
            producer:
              configuration:
                key:
                  serializer: org.apache.kafka.common.serialization.StringSerializer
                value:
                  serializer: io.confluent.kafka.streams.serdes.avro.SpecificAvroSerializer

正常运行应用程序时,AbstractMessageChannel.interceptorList为空,并且向代理发送消息正常。

运行测试(使用spring-cloud-stream-test-support活页夹)时,AbstractMessageChannel.interceptorList填充了MessageConverterConfigurer,并且正在使用内容类型序列化机制转换消息(Avro对象转换为JSON)。这是测试代码:

@RunWith(SpringRunner.class)
public class AccountServiceImplTest {

@Autowired
private AccountService accountService;

@Autowired
private MessageCollector messageCollector;

@Autowired
private MessageChannel accountSource;

@Test
public void create() {

    // Simplified code
    AccountCreationRequest accountCreationRequest = AccountCreationRequest.builder().company(company).subscription(subscription).user(user).build();

    accountCreationRequest = accountService.create(accountCreationRequest);

    Message<?> message = messageCollector.forChannel(accountSource).poll();
    // execute asserts on message
}

@TestConfiguration
@ComponentScan(basePackageClasses = TestSupportBinderAutoConfiguration.class)
static protected class AccountServiceImplTestConfiguration {

@EnableBinding({KafkaConfig.AccountBinding.class})

public interface AccountBinding {
    @Output("accountSource")
    MessageChannel accountSource();
}
}

我是否缺少一些禁用spring-cloud-stream流序列化机制的东西?

1 个答案:

答案 0 :(得分:0)

不要使用测试粘合剂;而是将Kafka活页夹与嵌入式kafka代理结合使用。