Best Testcontainers-java code snippet using org.testcontainers.containers.wait.strategy.Wait.forListeningPort
Source:DockerComposeHelper.java
...4import java.io.File;5import java.time.Duration;6import static com.kafkapingpong.infrastructure.helper.kafka.KafkaConstants.KAFKA_PORT;7import static java.lang.String.valueOf;8import static org.testcontainers.containers.wait.strategy.Wait.forListeningPort;9import static org.testcontainers.containers.wait.strategy.Wait.forLogMessage;10import static org.testcontainers.containers.wait.strategy.WaitAllStrategy.Mode.WITH_INDIVIDUAL_TIMEOUTS_ONLY;11public class DockerComposeHelper extends DockerComposeContainer<DockerComposeHelper> {12 private static final String POSTGRES = "postgres";13 private static final int POSTGRES_PORT = 5432;14 private static final String KAFKA = "kafka";15 private static final String ZOOKEEPER = "zookeeper";16 private static final int ZOOKEEPER_PORT = 2181;17 public DockerComposeHelper() {18 super(19 new File("./docker-compose.yml")20 );21 this22 .withLocalCompose(true)23 .withExposedService(POSTGRES, POSTGRES_PORT)24 .waitingFor(25 POSTGRES,26 new WaitAllStrategy(WITH_INDIVIDUAL_TIMEOUTS_ONLY)27 .withStrategy(forListeningPort())28 .withStrategy(forLogMessage(".*database system is ready to accept connections.*", 1)))29 .withExposedService(KAFKA, KAFKA_PORT)30 .waitingFor(KAFKA, new WaitAllStrategy(WITH_INDIVIDUAL_TIMEOUTS_ONLY)31 .withStrategy(forListeningPort())32 .withStrategy(forLogMessage(".*creating topics.*", 1))33 )34 .withExposedService(ZOOKEEPER, ZOOKEEPER_PORT)35 .waitingFor(ZOOKEEPER, new WaitAllStrategy(WITH_INDIVIDUAL_TIMEOUTS_ONLY)36 .withStrategy(forListeningPort())37 .withStrategy(forLogMessage(".*binding to port.*", 1))38 )39 .waitingFor("pingpong", new WaitAllStrategy()40 .withStrategy(forLogMessage(".+Started Application in .+ seconds.+", 1))41 .withStartupTimeout(Duration.ofMinutes(3))42 );43 }44}...
Source:DockerizedInfrastructure.java
...7import org.testcontainers.containers.DockerComposeContainer;8import org.testcontainers.containers.wait.strategy.WaitAllStrategy;9import org.testcontainers.containers.wait.strategy.WaitStrategy;10import static java.lang.String.format;11import static org.testcontainers.containers.wait.strategy.Wait.forListeningPort;12import static org.testcontainers.containers.wait.strategy.Wait.forLogMessage;13import static org.testcontainers.containers.wait.strategy.WaitAllStrategy.Mode.WITH_INDIVIDUAL_TIMEOUTS_ONLY;14@Component15class DockerizedInfrastructure {16 private static final File DOCKER_COMPOSE = new File("docker-compose.yml");17 private static WaitStrategy S3_STORAGE_WAIT_STRATEGY = new WaitAllStrategy(WITH_INDIVIDUAL_TIMEOUTS_ONLY)18 .withStrategy(forListeningPort())19 .withStrategy(forLogMessage(".*make_bucket.*", 1));20 private static final String S3_STORAGE_SERVICE = "s3-storage";21 private static final Integer S3_STORAGE_PORT = 4566;22 private static final String SPRING_S3_STORAGE_ENDPOINT = "s3-storage.endpoint";23 private final DockerComposeContainer dockerServices;24 public DockerizedInfrastructure(ConfigurableApplicationContext configurableApplicationContext) {25 dockerServices = new DockerComposeContainer(DOCKER_COMPOSE)26 .withLocalCompose(true)27 .waitingFor("s3-storage", S3_STORAGE_WAIT_STRATEGY)28 .withExposedService(S3_STORAGE_SERVICE, S3_STORAGE_PORT);29 dockerServices.start();30 TestPropertyValues.of(31 format("%s=%s", SPRING_S3_STORAGE_ENDPOINT, "http://localhost:" + dockerServices.getServicePort(S3_STORAGE_SERVICE, S3_STORAGE_PORT))32 ).applyTo(configurableApplicationContext.getEnvironment());...
forListeningPort
Using AI Code Generation
1package org.testcontainers.containers.wait.strategy;2import org.testcontainers.containers.ContainerLaunchException;3import org.testcontainers.containers.ContainerState;4import org.testcontainers.containers.wait.strategy.AbstractWaitStrategy;5import org.testcontainers.containers.wait.strategy.WaitStrategyTarget;6import java.time.Duration;7import java.util.Arrays;8import java.util.List;9import java.util.concurrent.TimeoutException;10import java.util.function.Predicate;11public class Wait extends AbstractWaitStrategy {12 public Wait() {13 }14 public static Wait forListeningPort() {15 return new Wait().withStrategy(WaitStrategyTarget::waitUntilListening);16 }17 public static Wait forLogMessage(String expectedLogMessage, Integer times) {18 return new Wait().withStrategy((target) -> {19 return target.waitUntilOutputLineMatches(expectedLogMessage, times);20 });21 }22 public static Wait forLogMessage(String expectedLogMessage, int times) {23 return forLogMessage(expectedLogMessage, Integer.valueOf(times));24 }25 public static Wait forLogMessage(String expectedLogMessage) {26 return forLogMessage(expectedLogMessage, 1);27 }28 public static Wait forLogMessage(Predicate<String> logMessagePredicate, Integer times) {29 return new Wait().withStrategy((target) -> {30 return target.waitUntilOutputLineMatches(logMessagePredicate, times);31 });32 }33 public static Wait forLogMessage(Predicate<String> logMessagePredicate, int times) {34 return forLogMessage(logMessagePredicate, Integer.valueOf(times));35 }36 public static Wait forLogMessage(Predicate<String> logMessagePredicate) {37 return forLogMessage(logMessagePredicate, 1);38 }39 public static Wait forHealthcheck() {40 return new Wait().withStrategy(WaitStrategyTarget::waitUntilContainerStarted);41 }42 public static Wait forHealthcheck(Predicate<ContainerState> healthcheckPredicate, Integer times) {43 return new Wait().withStrategy((target) -> {44 return target.waitUntilContainerStarted(healthcheckPredicate, times);45 });46 }47 public static Wait forHealthcheck(Predicate<ContainerState> healthcheckPredicate, int times) {48 return forHealthcheck(healthcheckPredicate, Integer.valueOf(times));49 }50 public static Wait forHealthcheck(Predicate<ContainerState> healthcheckPredicate) {51 return forHealthcheck(healthcheckPredicate, 1);52 }53 public static Wait forShellScript(String command,
forListeningPort
Using AI Code Generation
1import org.testcontainers.containers.GenericContainer;2import org.testcontainers.containers.wait.strategy.Wait;3import org.testcontainers.utility.DockerImageName;4public class Main {5 public static void main(String[] args) {6 try (GenericContainer container = new GenericContainer(DockerImageName.parse("quay.io/bitnami/mongodb:5.0.3-debian-10-r3"))7 .withExposedPorts(27017)8 .waitingFor(Wait.forListeningPort())) {9 container.start();10 System.out.println(container.getMappedPort(27017));11 }12 }13}
forListeningPort
Using AI Code Generation
1import org.testcontainers.containers.GenericContainer;2public class TestContainer {3 public static void main(String[] args) {4 GenericContainer container = new GenericContainer("mysql:5.7")5 .withExposedPorts(3306)6 .waitingFor(Wait.forListeningPort());7 container.start();8 System.out.println(container.getMappedPort(3306));9 }10}
forListeningPort
Using AI Code Generation
1import org.testcontainers.containers.wait.strategy.Wait;2import org.testcontainers.containers.wait.strategy.WaitStrategy;3import org.testcontainers.containers.wait.strategy.WaitStrategyTarget;4import org.testcontainers.containers.wait.strategy.WaitStrategyTarget.WaitStrategyTargetHostPortWaitStrategyTarget;5import org.testcontainers.containers.wait.strategy.WaitStrategyTarget.WaitStrategyTargetHostPortWaitStrategyTarget.WaitStrategyTargetHostPortWaitStrategyTargetHostPortWaitStrategyTarget;6import java.util.concurrent.TimeUnit;7public class WaitStrategyTest {8 public static void main(String[] args) {9 WaitStrategyTarget waitStrategyTarget = new WaitStrategyTargetHostPortWaitStrategyTargetHostPortWaitStrategyTarget(8000);10 WaitStrategy waitStrategy = Wait.forListeningPort().withStartupTimeout(100, TimeUnit.SECONDS);11 waitStrategy.waitUntilReady(waitStrategyTarget);12 }13}14org.testcontainers.containers.ContainerLaunchException: Timed out waiting for container port to open (8000 should be listening)
forListeningPort
Using AI Code Generation
1import org.testcontainers.containers.GenericContainer;2import org.testcontainers.containers.wait.strategy.Wait;3public class TestContainersDemo {4 public static void main(String[] args) {5 try (GenericContainer container = new GenericContainer("mysql:5.7.22")) {6 container.withExposedPorts(3306);7 container.withEnv("MYSQL_ROOT_PASSWORD", "password");8 container.withEnv("MYSQL_DATABASE", "test");9 container.waitingFor(Wait.forListeningPort());10 container.start();11 System.out.println("Container started");12 }13 }14}15import org.testcontainers.containers.GenericContainer;16import org.testcontainers.containers.wait.strategy.Wait;17public class TestContainersDemo {18 public static void main(String[] args) {19 try (GenericContainer container = new GenericContainer("mysql:5.7.22")) {20 container.withExposedPorts(3306);21 container.withEnv("MYSQL_ROOT_PASSWORD", "password");22 container.withEnv("MYSQL_DATABASE", "test");23 container.waitingFor(Wait.forListeningPort());24 container.start();25 System.out.println("Container started");26 }27 }28}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!