Skip to content

Commit

Permalink
refactor: 테스트 환경에서 캐싱을 사용하도록 하여 환경 통일
Browse files Browse the repository at this point in the history
  • Loading branch information
donghoony committed Dec 15, 2024
1 parent 35d7530 commit 7a137a5
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
7 changes: 7 additions & 0 deletions backend/src/test/java/reviewme/config/TestConfig.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package reviewme.config;

import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import reviewme.support.CacheCleaner;
import reviewme.support.DatabaseCleaner;

@TestConfiguration
Expand All @@ -11,4 +13,9 @@ public class TestConfig {
public DatabaseCleaner databaseCleaner() {
return new DatabaseCleaner();
}

@Bean
public CacheCleaner cacheCleaner(CacheManager cacheManager) {
return new CacheCleaner(cacheManager);
}
}
22 changes: 22 additions & 0 deletions backend/src/test/java/reviewme/support/CacheCleaner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package reviewme.support;

import java.util.Objects;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;

public class CacheCleaner {

private final CacheManager cacheManager;

public CacheCleaner(CacheManager cacheManager) {
this.cacheManager = cacheManager;
}

public void execute() {
cacheManager.getCacheNames()
.stream()
.map(cacheManager::getCache)
.filter(Objects::nonNull)
.forEach(Cache::clear);
}
}
15 changes: 15 additions & 0 deletions backend/src/test/java/reviewme/support/CacheCleanerExtension.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package reviewme.support;

import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.springframework.test.context.junit.jupiter.SpringExtension;

public class CacheCleanerExtension implements BeforeEachCallback {

@Override
public void beforeEach(ExtensionContext extensionContext) {
SpringExtension.getApplicationContext(extensionContext)
.getBean(CacheCleaner.class)
.execute();
}
}
2 changes: 1 addition & 1 deletion backend/src/test/java/reviewme/support/ServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@SpringBootTest(webEnvironment = WebEnvironment.NONE, classes = TestConfig.class)
@ExtendWith(DatabaseCleanerExtension.class)
@ExtendWith({DatabaseCleanerExtension.class, CacheCleanerExtension.class})
public @interface ServiceTest {
}
2 changes: 1 addition & 1 deletion backend/src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ spring:
flyway:
enabled: false
cache:
type: none
type: simple

springdoc:
swagger-ui:
Expand Down

0 comments on commit 7a137a5

Please sign in to comment.