1
1
package com .zzang .chongdae .event .service ;
2
2
3
+ import static org .mockito .ArgumentMatchers .any ;
3
4
import static org .mockito .Mockito .mock ;
4
5
import static org .mockito .Mockito .times ;
5
6
import static org .mockito .Mockito .verify ;
16
17
import org .junit .jupiter .api .Test ;
17
18
import org .springframework .beans .factory .annotation .Autowired ;
18
19
import org .springframework .boot .test .mock .mockito .MockBean ;
19
- import org .springframework .context .ApplicationEventPublisher ;
20
20
21
21
class FcmEventListenerTest extends ServiceTest {
22
22
23
23
@ Autowired
24
- ApplicationEventPublisher eventPublisher ;
24
+ TestEventPublisher testEventPublisher ;
25
25
26
26
@ MockBean
27
27
FcmEventListener eventListener ;
@@ -33,7 +33,7 @@ void should_executeEvent_when_publishParticipateEvent() {
33
33
ParticipateEvent event = mock (ParticipateEvent .class );
34
34
35
35
// when
36
- eventPublisher . publishEvent (event );
36
+ testEventPublisher . publishWithTransaction (event );
37
37
38
38
// then
39
39
verify (eventListener , times (1 )).handleParticipateEvent (event );
@@ -46,7 +46,7 @@ void should_executeEvent_when_publishParticipateCancelEvent() {
46
46
CancelParticipateEvent event = mock (CancelParticipateEvent .class );
47
47
48
48
// when
49
- eventPublisher . publishEvent (event );
49
+ testEventPublisher . publishWithTransaction (event );
50
50
51
51
// then
52
52
verify (eventListener , times (1 )).handleCancelParticipateEvent (event );
@@ -59,7 +59,7 @@ void should_executeEvent_when_publishSaveOfferingEvent() {
59
59
SaveOfferingEvent event = mock (SaveOfferingEvent .class );
60
60
61
61
// when
62
- eventPublisher . publishEvent (event );
62
+ testEventPublisher . publishWithoutTransaction (event );
63
63
64
64
// then
65
65
verify (eventListener , times (1 )).handleSaveOfferingEvent (event );
@@ -72,7 +72,7 @@ void should_executeEvent_when_publishDeleteOfferingEvent() {
72
72
DeleteOfferingEvent event = mock (DeleteOfferingEvent .class );
73
73
74
74
// when
75
- eventPublisher . publishEvent (event );
75
+ testEventPublisher . publishWithTransaction (event );
76
76
77
77
// then
78
78
verify (eventListener , times (1 )).handleDeleteOfferingEvent (event );
@@ -85,7 +85,7 @@ void should_executeEvent_when_publishSaveCommentEvent() {
85
85
SaveCommentEvent event = mock (SaveCommentEvent .class );
86
86
87
87
// when
88
- eventPublisher . publishEvent (event );
88
+ testEventPublisher . publishWithoutTransaction (event );
89
89
90
90
// then
91
91
verify (eventListener , times (1 )).handleSaveCommentEvent (event );
@@ -98,7 +98,7 @@ void should_executeEvent_when_publishUpdateStatusEvent() {
98
98
UpdateStatusEvent event = mock (UpdateStatusEvent .class );
99
99
100
100
// when
101
- eventPublisher . publishEvent (event );
101
+ testEventPublisher . publishWithTransaction (event );
102
102
103
103
// then
104
104
verify (eventListener , times (1 )).handleUpdateStatusEvent (event );
@@ -111,9 +111,25 @@ void should_executeEvent_when_publishLoginEvent() {
111
111
LoginEvent event = mock (LoginEvent .class );
112
112
113
113
// when
114
- eventPublisher . publishEvent (event );
114
+ testEventPublisher . publishWithTransaction (event );
115
115
116
116
// then
117
117
verify (eventListener , times (1 )).handleLoginEvent (event );
118
118
}
119
+
120
+ @ DisplayName ("이벤트 발행 후 예외가 발생한 경우 이벤트 로직을 실행하지 않는다." )
121
+ @ Test
122
+ void should_notExecuteEvent_when_throwException () {
123
+ // given
124
+ LoginEvent event = mock (LoginEvent .class );
125
+
126
+ // when
127
+ try {
128
+ testEventPublisher .publishWithTransactionThenThrowException (event );
129
+ } catch (Exception ignored ) {
130
+ }
131
+
132
+ // then
133
+ verify (eventListener , times (0 )).handleLoginEvent (any (LoginEvent .class ));
134
+ }
119
135
}
0 commit comments