ku-kim / springboot-redis-example

0 stars 0 forks source link

TestContainers를 활용하여 redis 사용하기 #2

Open ku-kim opened 2 years ago

ku-kim commented 2 years ago

📝 구현 목록


💡 참고자료

ku-kim commented 1 year ago

Testcontainers를 활용하여 redis 사용하기

  1. Testcontainers 의존성 추가
# build.gradle

dependencies {
    // ...

    // TestContainers
    testImplementation 'org.testcontainers:junit-jupiter:1.17.4'
}
  1. TestContainers에서 권장하는 logback 설정
# ./src/test/resources/logback-test.xml 
## ref : https://www.testcontainers.org/supported_docker_environment/logging_config/

<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="info">
        <appender-ref ref="STDOUT"/>
    </root>

    <logger name="org.testcontainers" level="INFO"/>
    <logger name="com.github.dockerjava" level="WARN"/>
    <logger name="com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.wire" level="OFF"/>
</configuration>
  1. redis 컨테이너 실행과 확인
// ./test/java/com/example/redis/RedisContainerTest.java
@Testcontainers
class RedisTestContaners {

    @Container
    public GenericContainer redis = new GenericContainer("redis:7.0.5")
            .withExposedPorts(6379);

    @Test
    void testContainers_실행확인() {
        assertThat(redis.getHost()).isEqualTo("localhost");
        assertThat(redis.getExposedPorts()).contains(6379);
    }
}