Skip to content

Commit 43912e5

Browse files
Use assertThatException for exception test (#4034)
Signed-off-by: Tran Ngoc Nhan <[email protected]>
1 parent 129e412 commit 43912e5

File tree

2 files changed

+9
-31
lines changed

2 files changed

+9
-31
lines changed

spring-kafka/src/test/java/org/springframework/kafka/listener/MissingTopicsTests.java

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
import org.springframework.kafka.test.context.EmbeddedKafka;
2828
import org.springframework.kafka.test.utils.KafkaTestUtils;
2929

30-
import static org.assertj.core.api.Assertions.assertThat;
31-
import static org.assertj.core.api.Assertions.fail;
30+
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
3231

3332
/**
3433
* @author Gary Russell
34+
* @author Ngoc Nhan
3535
* @since 2.2
3636
*
3737
*/
@@ -55,14 +55,8 @@ public void testMissingTopicCMLC() {
5555
ConcurrentMessageListenerContainer<Integer, String> container =
5656
new ConcurrentMessageListenerContainer<>(cf, containerProps);
5757
container.setBeanName("testMissing1");
58-
59-
try {
60-
container.start();
61-
fail("Expected exception");
62-
}
63-
catch (IllegalStateException e) {
64-
assertThat(e.getMessage()).contains("missingTopicsFatal");
65-
}
58+
assertThatIllegalStateException().isThrownBy(container::start)
59+
.withMessageContaining("missingTopicsFatal");
6660
}
6761

6862
@Test
@@ -75,13 +69,8 @@ public void testMissingTopicKMLC() {
7569
KafkaMessageListenerContainer<Integer, String> container =
7670
new KafkaMessageListenerContainer<>(cf, containerProps);
7771
container.setBeanName("testMissing2");
78-
try {
79-
container.start();
80-
fail("Expected exception");
81-
}
82-
catch (IllegalStateException e) {
83-
assertThat(e.getMessage()).contains("missingTopicsFatal");
84-
}
72+
assertThatIllegalStateException().isThrownBy(container::start)
73+
.withMessageContaining("missingTopicsFatal");
8574
container.getContainerProperties().setMissingTopicsFatal(false);
8675
container.start();
8776
container.stop();

spring-kafka/src/test/java/org/springframework/kafka/requestreply/ReplyingKafkaTemplateTests.java

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import java.util.concurrent.CountDownLatch;
3030
import java.util.concurrent.ExecutionException;
3131
import java.util.concurrent.TimeUnit;
32-
import java.util.concurrent.TimeoutException;
3332
import java.util.concurrent.atomic.AtomicInteger;
3433
import java.util.concurrent.atomic.AtomicReference;
3534

@@ -103,6 +102,7 @@
103102
* @author Nathan Xu
104103
* @author Soby Chacko
105104
* @author Mikhail Polivakha
105+
* @author Ngoc Nhan
106106
* @since 2.1.3
107107
*
108108
*/
@@ -903,19 +903,8 @@ void requestTimeoutWithMessage() throws Exception {
903903
CompletableFuture future = template.sendAndReceive(msg, Duration.ofMillis(10),
904904
new ParameterizedTypeReference<Foo>() {
905905
});
906-
try {
907-
future.get(10, TimeUnit.SECONDS);
908-
}
909-
catch (TimeoutException ex) {
910-
fail("get timed out");
911-
}
912-
catch (InterruptedException e) {
913-
Thread.currentThread().interrupt();
914-
fail("Interrupted");
915-
}
916-
catch (ExecutionException e) {
917-
assertThat(System.currentTimeMillis() - t1).isLessThan(3000L);
918-
}
906+
assertThatExceptionOfType(ExecutionException.class).isThrownBy(() -> future.get(10, TimeUnit.SECONDS));
907+
assertThat(System.currentTimeMillis() - t1).isLessThan(3000L);
919908
}
920909

921910
@Test

0 commit comments

Comments
 (0)