application test : testcontainers - docker compose
This commit is contained in:
11
application-test/app-test/docker-compose.yml
Normal file
11
application-test/app-test/docker-compose.yml
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
version: "3"
|
||||||
|
|
||||||
|
services:
|
||||||
|
study-db:
|
||||||
|
image: postgres
|
||||||
|
ports:
|
||||||
|
- 5432:5432
|
||||||
|
environment:
|
||||||
|
POSTGRES_PASSWORD: study
|
||||||
|
POSTGRES_USER: study
|
||||||
|
POSTGRES_DB: study
|
||||||
@@ -17,6 +17,7 @@ import org.springframework.context.ConfigurableApplicationContext;
|
|||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
import org.springframework.test.context.ActiveProfiles;
|
import org.springframework.test.context.ActiveProfiles;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.testcontainers.containers.DockerComposeContainer;
|
||||||
import org.testcontainers.containers.GenericContainer;
|
import org.testcontainers.containers.GenericContainer;
|
||||||
import org.testcontainers.containers.PostgreSQLContainer;
|
import org.testcontainers.containers.PostgreSQLContainer;
|
||||||
import org.testcontainers.containers.output.Slf4jLogConsumer;
|
import org.testcontainers.containers.output.Slf4jLogConsumer;
|
||||||
@@ -24,6 +25,7 @@ import org.testcontainers.containers.wait.strategy.Wait;
|
|||||||
import org.testcontainers.junit.jupiter.Container;
|
import org.testcontainers.junit.jupiter.Container;
|
||||||
import org.testcontainers.junit.jupiter.Testcontainers;
|
import org.testcontainers.junit.jupiter.Testcontainers;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
@@ -36,51 +38,66 @@ import static org.mockito.Mockito.times;
|
|||||||
@ExtendWith(MockitoExtension.class)
|
@ExtendWith(MockitoExtension.class)
|
||||||
@ActiveProfiles("test")
|
@ActiveProfiles("test")
|
||||||
@Testcontainers
|
@Testcontainers
|
||||||
@Slf4j
|
//@Slf4j
|
||||||
@ContextConfiguration(initializers = StudyServiceSpringBootTest.ContainerPropertyInitializer.class)
|
@ContextConfiguration(initializers = StudyServiceSpringBootTest.ContainerPropertyInitializer.class)
|
||||||
public class StudyServiceSpringBootTest {
|
public class StudyServiceSpringBootTest {
|
||||||
|
|
||||||
@Mock MemberService memberService;
|
@Mock MemberService memberService;
|
||||||
@Autowired StudyRepository studyRepository;
|
@Autowired StudyRepository studyRepository;
|
||||||
@Autowired Environment environment;
|
// @Autowired Environment environment;
|
||||||
@Value("${container.port}") int port;
|
@Value("${container.port}") int port;
|
||||||
|
|
||||||
@Container
|
@Container
|
||||||
static GenericContainer<?> postgreSQLContainer =
|
static DockerComposeContainer<?> composeContainer =
|
||||||
new GenericContainer<>("postgres")
|
new DockerComposeContainer<>(new File("src/test/resources/docker-compose.yml"))
|
||||||
.withEnv("POSTGRES_DB", "studytest");
|
.withExposedService("study-db", 5432);
|
||||||
|
|
||||||
@BeforeAll
|
// @Container
|
||||||
static void beforeAll() {
|
// static GenericContainer<?> postgreSQLContainer =
|
||||||
Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(log);
|
// new GenericContainer<>("postgres")
|
||||||
postgreSQLContainer.followOutput(logConsumer);
|
// .withEnv("POSTGRES_DB", "studytest");
|
||||||
}
|
|
||||||
|
// @BeforeAll
|
||||||
|
// static void beforeAll() {
|
||||||
|
// Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(log);
|
||||||
|
// postgreSQLContainer.followOutput(logConsumer);
|
||||||
|
// }
|
||||||
// @Container
|
// @Container
|
||||||
// static PostgreSQLContainer<?> postgreSQLContainer =
|
// static PostgreSQLContainer<?> postgreSQLContainer =
|
||||||
// new PostgreSQLContainer<>().withDatabaseName("studytest");
|
// new PostgreSQLContainer<>().withDatabaseName("studytest");
|
||||||
|
|
||||||
@BeforeEach
|
// @BeforeEach
|
||||||
void beforeEach() {
|
// void beforeEach() {
|
||||||
System.out.println("==========================");
|
// System.out.println("==========================");
|
||||||
System.out.println(postgreSQLContainer.getMappedPort(5432));
|
// System.out.println(postgreSQLContainer.getMappedPort(5432));
|
||||||
System.out.println("==========================");
|
// System.out.println("==========================");
|
||||||
System.out.println(environment.getProperty("container.port"));
|
// System.out.println(environment.getProperty("container.port"));
|
||||||
System.out.println("==========================");
|
// System.out.println("==========================");
|
||||||
System.out.println(port);
|
// System.out.println(port);
|
||||||
// System.out.println(postgreSQLContainer.getLogs());
|
//// System.out.println(postgreSQLContainer.getLogs());
|
||||||
studyRepository.deleteAll();
|
// studyRepository.deleteAll();
|
||||||
}
|
// }
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void test_51() {
|
void test_51() {
|
||||||
|
System.out.println(port);
|
||||||
|
System.out.println("==============");
|
||||||
System.out.println("테스트 완료!!");
|
System.out.println("테스트 완료!!");
|
||||||
}
|
}
|
||||||
|
|
||||||
static class ContainerPropertyInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
|
static class ContainerPropertyInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
|
||||||
@Override
|
@Override
|
||||||
public void initialize(ConfigurableApplicationContext context) {
|
public void initialize(ConfigurableApplicationContext context) {
|
||||||
TestPropertyValues.of("container.port=" + postgreSQLContainer.getMappedPort(5432))
|
TestPropertyValues.of("container.port=" + composeContainer.getServicePort("study-db", 5432))
|
||||||
.applyTo(context.getEnvironment());
|
.applyTo(context.getEnvironment());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// static class ContainerPropertyInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
|
||||||
|
// @Override
|
||||||
|
// public void initialize(ConfigurableApplicationContext context) {
|
||||||
|
// TestPropertyValues.of("container.port=" + postgreSQLContainer.getMappedPort(5432))
|
||||||
|
// .applyTo(context.getEnvironment());
|
||||||
|
// }
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
version: "3"
|
||||||
|
|
||||||
|
services:
|
||||||
|
study-db:
|
||||||
|
image: postgres
|
||||||
|
ports:
|
||||||
|
- 5432
|
||||||
|
environment:
|
||||||
|
POSTGRES_PASSWORD: study
|
||||||
|
POSTGRES_USER: study
|
||||||
|
POSTGRES_DB: study
|
||||||
Reference in New Issue
Block a user