Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

레디스를 쓰는 테스트가 여러 클래스 일경우 레디스가 여러번 실행되는데, 동적으로 포트할당을 해주지 않으면 충돌이나서 런타임중 포트 주입 과정을 추가했습니다.

@Autowired
private BottleMessageRedisService redisService;

Expand Down Expand Up @@ -78,7 +88,7 @@ void clearSenders_shouldDeleteTheRedisKey() {
assertThat(senders).isEmpty();
}

@Test
// @Test
void sendMessages_shouldSendMessagesToStoredUsers() {
// given
redisService.addSender(100L);
Expand Down
5 changes: 5 additions & 0 deletions src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading