尽管存在额外的内置错误,但带有 Spring Cloud Contract 的 CDC 测试始终是绿色的为什么?

问题描述

我想尝试 Spring Cloud Contract (SCC) 并创建了一个小项目,请参阅 GIT

我有额外的内置错误来测试 SCC。

但是如果我运行项目 ./gradlew build

  • 自动生成特定合约的测试
  • 并且构建始终是绿色的

有人可以帮我解决这个问题吗?我期望这个 CDC 应该失败。

谢谢。

这是我的合同:

package contracts.tse

import org.springframework.cloud.contract.spec.Contract

Contract.make {
    label("order_placed")
    input {
        triggeredBy("sendAccepted()")
    }
    outputMessage {
        sentTo("channel")
        body(
            orderId: "ddd",)
        headers {
            header("type","order-placed")
            messagingContentType(applicationjson())
        }
    }
}

这是我的 BaseClass 已经有内置错误

@SpringBoottest(classes = TseBase.Config.class)
@AutoConfigureMessageVerifier
public class TseBase {
    private static final String VALID_PERSONAL_ID = "86010197600";
    private static final String INVALID_PERSONAL_ID = "86010156812";

    @Autowired
    private OrderRabbitMqEventPublisherImpl publisher;

    public void sendAccepted() {
        //FIXME: should fail,wrong orderId
        publisher.publish(new OrderPlacedEvent("test_order_id"));
    }

    @Configuration
    @ImportAutoConfiguration({ TestSupportBinderAutoConfiguration.class,MessageCollectorAutoConfiguration.class,JacksonAutoConfiguration.class })
    @EnableBinding(Source.class)
    static class Config {
        @Bean
        OrderRabbitMqEventPublisherImpl publisher(Source source) {
            return new OrderRabbitMqEventPublisherImpl(source);
        }
    }
}

这是我的发布者的实现,已经存在内置错误

// FIXME: should fail here
//@Component
public class OrderRabbitMqEventPublisherImpl implements OrderPublisher {

    private final Source source;

    public OrderRabbitMqEventPublisherImpl(final Source source) {
        this.source = source;
    }

    @Override
    public void publish(final OrderPlacedEvent orderPlacedEvent) {
        Map<String,Object> headers = new HashMap<>();
        headers.put("type",orderPlacedEvent.getClass().getName());
        // FIXME: should fail here
//        this.source.output().send(new Genericmessage<>(orderPlacedEvent,headers));
    }
}

这是我生成的测试。

@SuppressWarnings("rawtypes")
public class TseTest extends TseBase {
    @Inject ContractVerifierMessaging contractVerifierMessaging;
    @Inject ContractVerifierObjectMapper contractVerifierObjectMapper;

    @Test
    public void validate_shouldSendAOrderPlacedEvent() throws Exception {
        // when:
            sendAccepted();

        // then:
            ContractVerifierMessage response = contractVerifierMessaging.receive("channel",contract(this,"shouldSendAOrderPlacedEvent.yml"));
            assertthat(response).isNotNull();

        // and:
            assertthat(response.getHeader("type")).isNotNull();
            assertthat(response.getHeader("type").toString()).isEqualTo("order-placed");
            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("['orderId']").isEqualTo("ddd");
    }

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)