1
+ package com .baeldung .automq ;
2
+
3
+ import org .awaitility .Awaitility ;
4
+ import org .awaitility .Durations ;
5
+ import org .junit .jupiter .api .BeforeAll ;
6
+ import org .junit .jupiter .api .Test ;
7
+ import org .junit .jupiter .api .TestInstance ;
8
+ import org .junit .jupiter .api .extension .ExtendWith ;
9
+ import org .springframework .beans .factory .annotation .Autowired ;
10
+ import org .springframework .beans .factory .annotation .Value ;
11
+ import org .springframework .boot .test .context .SpringBootTest ;
12
+ import org .springframework .boot .test .system .CapturedOutput ;
13
+ import org .springframework .boot .test .system .OutputCaptureExtension ;
14
+ import org .springframework .context .annotation .Import ;
15
+ import org .springframework .kafka .core .KafkaTemplate ;
16
+
17
+ import java .util .concurrent .TimeUnit ;
18
+
19
+ @ SpringBootTest
20
+ @ TestInstance (TestInstance .Lifecycle .PER_CLASS )
21
+ @ ExtendWith (OutputCaptureExtension .class )
22
+ @ Import (TestcontainersConfiguration .class )
23
+ class UserOnboardingInitiatedListenerLiveTest {
24
+
25
+ @ Autowired
26
+ private KafkaTemplate <String , User > kafkaTemplate ;
27
+
28
+ @ Value ("${com.baeldung.topic.onboarding-initiated}" )
29
+ private String onboardingInitiatedTopic ;
30
+
31
+ @ BeforeAll
32
+ void setUp (CapturedOutput capturedOutput ) {
33
+ String expectedLog = "partitions assigned" ;
34
+ Awaitility
35
+ .await ()
36
+ .atMost (Durations .ONE_MINUTE )
37
+ .pollDelay (Durations .ONE_SECOND )
38
+ .until (() -> capturedOutput .getAll ().contains (expectedLog ));
39
+ }
40
+
41
+ @ Test
42
+ void whenMessagePublishedToTopic_thenProcessedByListener (CapturedOutput capturedOutput ) {
43
+ User user =
new User (
"[email protected] " );
44
+ kafkaTemplate .send (onboardingInitiatedTopic , user );
45
+
46
+ String expectedConsumerLog = String .format ("Dispatching user account confirmation email to %s" , user .email ());
47
+ Awaitility
48
+ .await ()
49
+ .atMost (1 , TimeUnit .SECONDS )
50
+ .until (() -> capturedOutput .getAll ().contains (expectedConsumerLog ));
51
+ }
52
+
53
+ }
0 commit comments