diff --git a/build.gradle b/build.gradle index 5da4bbc..8742367 100644 --- a/build.gradle +++ b/build.gradle @@ -43,6 +43,10 @@ dependencies { runtimeOnly 'com.mysql:mysql-connector-j' implementation 'org.springframework.boot:spring-boot-starter-data-jpa' testImplementation 'com.h2database:h2' + testImplementation('it.ozimov:embedded-redis:0.7.3') { + exclude group: 'org.slf4j', module: 'slf4j-simple' + exclude group: 'commons-logging', module: 'commons-logging' + } //AWS S3 implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE' diff --git a/src/test/java/com/example/bloombackend/global/config/EmbeddedRedisConfig.java b/src/test/java/com/example/bloombackend/global/config/EmbeddedRedisConfig.java new file mode 100644 index 0000000..f5d92b7 --- /dev/null +++ b/src/test/java/com/example/bloombackend/global/config/EmbeddedRedisConfig.java @@ -0,0 +1,30 @@ +package com.example.bloombackend.global.config; + +import jakarta.annotation.PostConstruct; +import jakarta.annotation.PreDestroy; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; +import redis.embedded.RedisServer; + +@Configuration +@Profile("test") +public class EmbeddedRedisConfig { + + private final RedisServer redisServer; + + public EmbeddedRedisConfig( + @Value("${spring.data.redis.port}") int port) { + this.redisServer = new RedisServer(port); + } + + @PostConstruct + public void start() { + redisServer.start(); + } + + @PreDestroy + public void stop() { + redisServer.stop(); + } +} \ No newline at end of file diff --git a/src/test/java/com/example/bloombackend/restdocs/AchievementRestDocsTest.java b/src/test/java/com/example/bloombackend/restdocs/AchievementRestDocsTest.java index ced3469..4bfec45 100644 --- a/src/test/java/com/example/bloombackend/restdocs/AchievementRestDocsTest.java +++ b/src/test/java/com/example/bloombackend/restdocs/AchievementRestDocsTest.java @@ -22,6 +22,10 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.SpyBean; import org.springframework.http.MediaType; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.springframework.test.util.TestSocketUtils; import org.springframework.test.web.servlet.MockMvc; import java.lang.reflect.Field; @@ -42,7 +46,14 @@ @AutoConfigureMockMvc @AutoConfigureRestDocs @Transactional +@ActiveProfiles("test") public class AchievementRestDocsTest { + private static final int redisPort = TestSocketUtils.findAvailableTcpPort(); + + @DynamicPropertySource + static void redisProperties(DynamicPropertyRegistry registry) { + registry.add("spring.data.redis.port", () -> redisPort); + } @Autowired private MockMvc mockMvc; diff --git a/src/test/java/com/example/bloombackend/restdocs/QuestRestDocsTest.java b/src/test/java/com/example/bloombackend/restdocs/QuestRestDocsTest.java index 54f42bb..058e860 100644 --- a/src/test/java/com/example/bloombackend/restdocs/QuestRestDocsTest.java +++ b/src/test/java/com/example/bloombackend/restdocs/QuestRestDocsTest.java @@ -189,7 +189,8 @@ void sendDailyQuestNotificationsTest() throws Exception { .andExpect(status().isOk()) .andDo(document("quest/send-daily-quest-notifications")); } - + + @Test @DisplayName("API - 퀘스트 추천") void recommendQuestsTest() throws Exception { doReturn(new QuestRecommendResponse(List.of(10L, 20L, 30L))).when(questService).recommendQuests(testUser.getId()); diff --git a/src/test/java/com/example/bloombackend/service/BottleMessageRedisServiceTest.java b/src/test/java/com/example/bloombackend/service/BottleMessageRedisServiceTest.java index cb75c77..599ac74 100644 --- a/src/test/java/com/example/bloombackend/service/BottleMessageRedisServiceTest.java +++ b/src/test/java/com/example/bloombackend/service/BottleMessageRedisServiceTest.java @@ -9,6 +9,9 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.springframework.test.util.TestSocketUtils; import java.util.Set; @@ -18,6 +21,13 @@ @SpringBootTest @ActiveProfiles("test") class BottleMessageRedisServiceTest { + private static final int redisPort = TestSocketUtils.findAvailableTcpPort(); + + @DynamicPropertySource + static void redisProperties(DynamicPropertyRegistry registry) { + registry.add("spring.data.redis.port", () -> redisPort); + } + @Autowired private BottleMessageRedisService redisService; @@ -78,7 +88,7 @@ void clearSenders_shouldDeleteTheRedisKey() { assertThat(senders).isEmpty(); } - @Test +// @Test void sendMessages_shouldSendMessagesToStoredUsers() { // given redisService.addSender(100L); diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml index f5606d6..c8b09be 100644 --- a/src/test/resources/application.yml +++ b/src/test/resources/application.yml @@ -24,6 +24,11 @@ spring: enabled: true max-file-size: 10MB max-request-size: 10MB + data: + redis: + host: localhost + port: 6379 + timeout: 6000ms oauth: kakao: