From aa4db9628922f2698c64601c3234dddfd39ac986 Mon Sep 17 00:00:00 2001 From: Igor Stepanov Date: Tue, 5 Jul 2016 17:21:13 -0400 Subject: [PATCH] GH-134: AssertJ style for thrown assertions **Cherry-pick to 1.0.x** --- .../kafka/core/KafkaTemplateTests.java | 22 +++++++------------ 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/spring-kafka/src/test/java/org/springframework/kafka/core/KafkaTemplateTests.java b/spring-kafka/src/test/java/org/springframework/kafka/core/KafkaTemplateTests.java index 760c13cf..6aad0032 100644 --- a/spring-kafka/src/test/java/org/springframework/kafka/core/KafkaTemplateTests.java +++ b/spring-kafka/src/test/java/org/springframework/kafka/core/KafkaTemplateTests.java @@ -17,7 +17,7 @@ package org.springframework.kafka.core; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.fail; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.mockito.Mockito.mock; import static org.springframework.kafka.test.assertj.KafkaConditions.key; import static org.springframework.kafka.test.assertj.KafkaConditions.partition; @@ -48,7 +48,6 @@ import org.springframework.messaging.support.MessageBuilder; import org.springframework.util.concurrent.ListenableFuture; import org.springframework.util.concurrent.ListenableFutureCallback; - /** * @author Gary Russell * @author Artem Bilan @@ -125,7 +124,7 @@ public class KafkaTemplateTests { @Test public void withListener() throws Exception { Map senderProps = KafkaTestUtils.producerProps(embeddedKafka); - ProducerFactory pf = new DefaultKafkaProducerFactory(senderProps); + ProducerFactory pf = new DefaultKafkaProducerFactory<>(senderProps); KafkaTemplate template = new KafkaTemplate<>(pf); template.setDefaultTopic(INT_KEY_TOPIC); final CountDownLatch latch = new CountDownLatch(1); @@ -152,7 +151,7 @@ public class KafkaTemplateTests { @Test public void testWithCallback() throws Exception { Map senderProps = KafkaTestUtils.producerProps(embeddedKafka); - ProducerFactory pf = new DefaultKafkaProducerFactory(senderProps); + ProducerFactory pf = new DefaultKafkaProducerFactory<>(senderProps); KafkaTemplate template = new KafkaTemplate<>(pf, true); template.setDefaultTopic(INT_KEY_TOPIC); ListenableFuture> future = template.sendDefault("foo"); @@ -180,12 +179,12 @@ public class KafkaTemplateTests { @Test public void testTemplateDisambiguation() throws Exception { Map senderProps = KafkaTestUtils.producerProps(embeddedKafka); - DefaultKafkaProducerFactory pf = new DefaultKafkaProducerFactory(senderProps); + DefaultKafkaProducerFactory pf = new DefaultKafkaProducerFactory<>(senderProps); pf.setKeySerializer(new StringSerializer()); KafkaTemplate template = new KafkaTemplate<>(pf, true); template.setDefaultTopic(STRING_KEY_TOPIC); Map consumerProps = KafkaTestUtils.consumerProps("testTString", "false", embeddedKafka); - DefaultKafkaConsumerFactory cf = new DefaultKafkaConsumerFactory(consumerProps); + DefaultKafkaConsumerFactory cf = new DefaultKafkaConsumerFactory<>(consumerProps); cf.setKeyDeserializer(new StringDeserializer()); Consumer consumer = cf.createConsumer(); embeddedKafka.consumeFromAnEmbeddedTopic(consumer, STRING_KEY_TOPIC); @@ -201,14 +200,9 @@ public class KafkaTemplateTests { @SuppressWarnings({"rawtypes", "unchecked"}) public void flushWithoutSend() throws Exception { KafkaTemplate template = new KafkaTemplate(mock(ProducerFactory.class)); - try { - template.flush(); - fail("IllegalStateException expected"); - } - catch (Exception e) { - assertThat(e).isInstanceOf(IllegalStateException.class); - assertThat(e.getMessage()).isEqualTo("'producer' must not be null for flushing."); - } + assertThatExceptionOfType(IllegalStateException.class) + .isThrownBy(template::flush) + .withMessageContaining("'producer' must not be null for flushing."); } }