diff --git a/backend/src/test/java/reviewme/config/TestConfig.java b/backend/src/test/java/reviewme/config/TestConfig.java index f339dd641..e3a05bb95 100644 --- a/backend/src/test/java/reviewme/config/TestConfig.java +++ b/backend/src/test/java/reviewme/config/TestConfig.java @@ -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 @@ -11,4 +13,9 @@ public class TestConfig { public DatabaseCleaner databaseCleaner() { return new DatabaseCleaner(); } + + @Bean + public CacheCleaner cacheCleaner(CacheManager cacheManager) { + return new CacheCleaner(cacheManager); + } } diff --git a/backend/src/test/java/reviewme/support/CacheCleaner.java b/backend/src/test/java/reviewme/support/CacheCleaner.java new file mode 100644 index 000000000..7c96acfe6 --- /dev/null +++ b/backend/src/test/java/reviewme/support/CacheCleaner.java @@ -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); + } +} diff --git a/backend/src/test/java/reviewme/support/CacheCleanerExtension.java b/backend/src/test/java/reviewme/support/CacheCleanerExtension.java new file mode 100644 index 000000000..e941b6e83 --- /dev/null +++ b/backend/src/test/java/reviewme/support/CacheCleanerExtension.java @@ -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(); + } +} diff --git a/backend/src/test/java/reviewme/support/ServiceTest.java b/backend/src/test/java/reviewme/support/ServiceTest.java index 34ae4b4fd..c3838a35f 100644 --- a/backend/src/test/java/reviewme/support/ServiceTest.java +++ b/backend/src/test/java/reviewme/support/ServiceTest.java @@ -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 { } diff --git a/backend/src/test/resources/application.yml b/backend/src/test/resources/application.yml index ccbe2e2ff..541b9ff06 100644 --- a/backend/src/test/resources/application.yml +++ b/backend/src/test/resources/application.yml @@ -14,7 +14,7 @@ spring: flyway: enabled: false cache: - type: none + type: simple springdoc: swagger-ui: