|
1 | 1 | package com.techfork.personalization.integration; |
2 | 2 |
|
| 3 | +import com.techfork.domain.recommendation.listener.PersonalizedProfileGeneratedEventListener; |
3 | 4 | import com.techfork.domain.recommendation.service.RecommendationService; |
4 | | -import com.techfork.global.common.IntegrationTestBase; |
5 | 5 | import com.techfork.personalization.application.event.PersonalizedProfileGeneratedEvent; |
6 | 6 | import com.techfork.useraccount.application.query.lookup.UserLookupService; |
7 | 7 | import com.techfork.useraccount.domain.User; |
| 8 | +import org.junit.jupiter.api.BeforeEach; |
8 | 9 | import org.junit.jupiter.api.DisplayName; |
| 10 | +import org.junit.jupiter.api.Tag; |
9 | 11 | import org.junit.jupiter.api.Test; |
10 | 12 | import org.springframework.beans.factory.annotation.Autowired; |
11 | 13 | import org.springframework.context.ApplicationEventPublisher; |
12 | | -import org.springframework.test.context.bean.override.mockito.MockitoBean; |
| 14 | +import org.springframework.context.annotation.Bean; |
| 15 | +import org.springframework.context.annotation.Configuration; |
| 16 | +import org.springframework.jdbc.datasource.DataSourceTransactionManager; |
| 17 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; |
| 18 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; |
| 19 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 20 | +import org.springframework.transaction.PlatformTransactionManager; |
| 21 | +import org.springframework.transaction.annotation.EnableTransactionManagement; |
13 | 22 | import org.springframework.transaction.support.TransactionTemplate; |
14 | 23 |
|
15 | 24 | import java.util.Arrays; |
16 | 25 | import java.util.List; |
17 | 26 |
|
| 27 | +import javax.sql.DataSource; |
| 28 | + |
18 | 29 | import static org.mockito.ArgumentMatchers.argThat; |
19 | 30 | import static org.mockito.ArgumentMatchers.eq; |
20 | 31 | import static org.mockito.BDDMockito.given; |
21 | 32 | import static org.mockito.Mockito.mock; |
22 | 33 | import static org.mockito.Mockito.never; |
| 34 | +import static org.mockito.Mockito.reset; |
23 | 35 | import static org.mockito.Mockito.verify; |
24 | 36 | import static org.mockito.Mockito.verifyNoInteractions; |
25 | 37 |
|
26 | | -class PersonalizedProfileGeneratedAfterCommitIntegrationTest extends IntegrationTestBase { |
| 38 | +@Tag("integration") |
| 39 | +@SpringJUnitConfig(PersonalizedProfileGeneratedAfterCommitIntegrationTest.TestConfig.class) |
| 40 | +class PersonalizedProfileGeneratedAfterCommitIntegrationTest { |
27 | 41 |
|
28 | 42 | @Autowired |
29 | 43 | private ApplicationEventPublisher eventPublisher; |
30 | 44 |
|
31 | 45 | @Autowired |
32 | 46 | private TransactionTemplate transactionTemplate; |
33 | 47 |
|
34 | | - @MockitoBean |
| 48 | + @Autowired |
35 | 49 | private UserLookupService userLookupService; |
36 | 50 |
|
37 | | - @MockitoBean |
| 51 | + @Autowired |
38 | 52 | private RecommendationService recommendationService; |
39 | 53 |
|
| 54 | + @BeforeEach |
| 55 | + void setUp() { |
| 56 | + reset(userLookupService, recommendationService); |
| 57 | + } |
| 58 | + |
40 | 59 | @Test |
41 | 60 | @DisplayName("프로필 생성 이벤트는 실제 트랜잭션 커밋 이후 추천 생성을 실행한다") |
42 | 61 | void profileGeneratedEvent_CommittedTransaction_GeneratesRecommendationAfterCommit() { |
@@ -80,4 +99,45 @@ void profileGeneratedEvent_RolledBackTransaction_DoesNotGenerateRecommendation() |
80 | 99 |
|
81 | 100 | verifyNoInteractions(userLookupService, recommendationService); |
82 | 101 | } |
| 102 | + |
| 103 | + @Configuration |
| 104 | + @EnableTransactionManagement |
| 105 | + static class TestConfig { |
| 106 | + |
| 107 | + @Bean |
| 108 | + PersonalizedProfileGeneratedEventListener personalizedProfileGeneratedEventListener( |
| 109 | + UserLookupService userLookupService, |
| 110 | + RecommendationService recommendationService |
| 111 | + ) { |
| 112 | + return new PersonalizedProfileGeneratedEventListener(userLookupService, recommendationService); |
| 113 | + } |
| 114 | + |
| 115 | + @Bean |
| 116 | + UserLookupService userLookupService() { |
| 117 | + return mock(UserLookupService.class); |
| 118 | + } |
| 119 | + |
| 120 | + @Bean |
| 121 | + RecommendationService recommendationService() { |
| 122 | + return mock(RecommendationService.class); |
| 123 | + } |
| 124 | + |
| 125 | + @Bean |
| 126 | + DataSource dataSource() { |
| 127 | + return new EmbeddedDatabaseBuilder() |
| 128 | + .generateUniqueName(true) |
| 129 | + .setType(EmbeddedDatabaseType.H2) |
| 130 | + .build(); |
| 131 | + } |
| 132 | + |
| 133 | + @Bean |
| 134 | + PlatformTransactionManager transactionManager(DataSource dataSource) { |
| 135 | + return new DataSourceTransactionManager(dataSource); |
| 136 | + } |
| 137 | + |
| 138 | + @Bean |
| 139 | + TransactionTemplate transactionTemplate(PlatformTransactionManager transactionManager) { |
| 140 | + return new TransactionTemplate(transactionManager); |
| 141 | + } |
| 142 | + } |
83 | 143 | } |
0 commit comments