尽管有额外的内置错误,cdc与SpringCloud的合同测试总是绿色的为什么?

xmq68pz9  于 2021-07-16  发布在  Java
关注(0)|答案(0)|浏览(167)

我想尝试springcloudcontract(scc),并创建了一个小项目see git。
我有额外的内置错误来测试scc。
但如果我负责这个项目 ./gradlew build .
特定合同的测试将自动生成
建筑总是绿色的
有人能帮我吗?我希望这个疾病控制中心会失败。
谢谢。
这是我的合同:

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())
        }
    }
}

这是我的基类,已经有内置错误

@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);
        }
    }
}

下面是我的publisher的实现,其中已经有内置错误

// 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");
    }

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题