From 75d476aaf2711cefc4c312204e041cd3ef76d1c4 Mon Sep 17 00:00:00 2001 From: Michal Krzywanski Date: Mon, 9 Aug 2021 17:07:07 +0200 Subject: [PATCH] changing expected message serialization --- .../java/com/example/ApplicationTests.java | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/consumer_rabbit_middleware/src/test/java/com/example/ApplicationTests.java b/consumer_rabbit_middleware/src/test/java/com/example/ApplicationTests.java index 4e7e9929a..6a51b04ab 100644 --- a/consumer_rabbit_middleware/src/test/java/com/example/ApplicationTests.java +++ b/consumer_rabbit_middleware/src/test/java/com/example/ApplicationTests.java @@ -17,14 +17,14 @@ package com.example; // remove::start[] + +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; import java.util.concurrent.TimeUnit; import javax.annotation.Nullable; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; import org.assertj.core.api.BDDAssertions; import org.awaitility.Awaitility; import org.junit.jupiter.api.Test; @@ -51,7 +51,7 @@ import org.springframework.test.context.DynamicPropertyRegistry; import org.springframework.test.context.DynamicPropertySource; -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = { TestConfig.class, Application.class }, properties = "stubrunner.amqp.mockConnection=false") +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = {TestConfig.class, Application.class}, properties = "stubrunner.amqp.mockConnection=false") @AutoConfigureStubRunner(ids = "com.example:beer-api-producer-rabbit-middleware", stubsMode = StubRunnerProperties.StubsMode.LOCAL) @Testcontainers @ActiveProfiles("test") @@ -59,7 +59,8 @@ public class ApplicationTests { // remove::start[] - @Container static RabbitMQContainer rabbit = new RabbitMQContainer(); + @Container + static RabbitMQContainer rabbit = new RabbitMQContainer(); @DynamicPropertySource static void rabbitProperties(DynamicPropertyRegistry registry) { @@ -78,7 +79,7 @@ public void contextLoads() { Awaitility.await().untilAsserted(() -> { BDDAssertions.then(this.application.storedFoo).isNotNull(); - BDDAssertions.then(this.application.storedFoo.getFoo()).contains("example"); + BDDAssertions.then(this.application.storedFoo.getFoo()).isEqualTo("example"); }); } // remove::end[] @@ -105,7 +106,7 @@ public Message receive(String destination, YamlContract contract) { @Override public void send(Message message, String destination, @Nullable YamlContract contract) { - rabbitTemplate.send(destination, message); + rabbitTemplate.send(destination, message); } @Override @@ -114,12 +115,12 @@ public void send(T payload, Map headers, String destination, MessageProperties messageProperties = new MessageProperties(); newHeaders.forEach(messageProperties::setHeader); log.info("Sending a message to destination [{}] with routing key", destination); - try { - Message message = MessageBuilder.withBody(new ObjectMapper().writeValueAsBytes(payload)).andProperties(messageProperties).build(); + if (payload instanceof String) { + String json = (String) payload; + Message message = MessageBuilder.withBody(json.getBytes(StandardCharsets.UTF_8)).andProperties(messageProperties).build(); send(message, destination, contract); - } - catch (JsonProcessingException e) { - throw new IllegalStateException(e); + } else { + throw new IllegalStateException("Payload is not a String"); } } };