import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.support.TestPropertySourceUtils;
import org.testcontainers.containers.GenericContainer;
+import org.testcontainers.containers.output.Slf4jLogConsumer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
}
}
+ private static final int MONGODB_PORT = 27017;
@Container
- private static final GenericContainer mongodb =
+ private static final GenericContainer CONTAINER =
new GenericContainer("mongo:6")
.withEnv("MONGO_INITDB_ROOT_USERNAME", "mongo")
.withEnv("MONGO_INITDB_ROOT_PASSWORD", "mongo")
- .withEnv("MONGO_INITDB_DATABASE", "test");
+ .withEnv("MONGO_INITDB_DATABASE", "test")
+ .withExposedPorts(MONGODB_PORT);
public static class DataSourceInitializer
implements ApplicationContextInitializer<ConfigurableApplicationContext>
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(
applicationContext,
"spring.data.mongodb.host=localhost",
- "spring.data.mongodb.port=27017",
+ "spring.data.mongodb.port=" + CONTAINER.getMappedPort(MONGODB_PORT),
"spring.data.mongodb.username=mongo",
"spring.data.mongodb.password=mongo",
"spring.data.mongodb.database=test");
- }
+
+ Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(log);
+ CONTAINER.followOutput(logConsumer); }
}
}