Best Testcontainers-java code snippet using org.testcontainers.containers.startupcheck.StartupCheckStrategyTest.waitForHello
Source:StartupCheckStrategyTest.java
...18@RunWith(Suite.class)19@Suite.SuiteClasses({OneShotStrategyTest.class, IndefiniteOneShotStrategyTest.class, MinimumDurationStrategyTest.class})20public class StartupCheckStrategyTest {21 private static final String HELLO_TESTCONTAINERS = "Hello Testcontainers!";22 private static void waitForHello(GenericContainer container) throws TimeoutException {23 WaitingConsumer consumer = new WaitingConsumer();24 container.followOutput(consumer, STDOUT);25 consumer.waitUntil(frame ->26 frame.getUtf8String().contains(HELLO_TESTCONTAINERS), 30, TimeUnit.SECONDS);27 }28 public static class OneShotStrategyTest {29 @Rule30 // withOneShotStrategy {31 public GenericContainer<?> bboxWithOneShot = new GenericContainer<>(DockerImageName.parse("busybox:1.31.1"))32 .withCommand(String.format("echo %s", HELLO_TESTCONTAINERS))33 .withStartupCheckStrategy(34 new OneShotStartupCheckStrategy().withTimeout(Duration.ofSeconds(3))35 );36 // }37 @SneakyThrows38 @Test39 public void testCommandIsExecuted() {40 waitForHello(bboxWithOneShot);41 assertThat(bboxWithOneShot.isRunning()).isFalse();42 }43 }44 public static class IndefiniteOneShotStrategyTest {45 @Rule46 // withIndefiniteOneShotStrategy {47 public GenericContainer<?> bboxWithIndefiniteOneShot = new GenericContainer<>(DockerImageName.parse("busybox:1.31.1"))48 .withCommand("sh", "-c", String.format("sleep 5 && echo \"%s\"", HELLO_TESTCONTAINERS))49 .withStartupCheckStrategy(50 new IndefiniteWaitOneShotStartupCheckStrategy()51 );52 // }53 @SneakyThrows54 @Test55 public void testCommandIsExecuted() {56 waitForHello(bboxWithIndefiniteOneShot);57 assertThat(bboxWithIndefiniteOneShot.isRunning()).isFalse();58 }59 }60 public static class MinimumDurationStrategyTest {61 @Rule62 // withMinimumDurationStrategy {63 public GenericContainer<?> bboxWithMinimumDuration = new GenericContainer<>(DockerImageName.parse("busybox:1.31.1"))64 .withCommand("sh", "-c", String.format("sleep 5 && echo \"%s\"", HELLO_TESTCONTAINERS))65 .withStartupCheckStrategy(66 new MinimumDurationRunningStartupCheckStrategy(Duration.ofSeconds(1))67 );68 // }69 @SneakyThrows70 @Test71 public void testCommandIsExecuted() {72 waitForHello(bboxWithMinimumDuration);73 assertThat(bboxWithMinimumDuration.isRunning()).isTrue();74 }75 }76}...
waitForHello
Using AI Code Generation
1public class StartupCheckStrategyTest {2 public void waitForHello() throws Exception {3 try (GenericContainer container = new GenericContainer("alpine:3.3")4 .withStartupCheckStrategy(new StartupCheckStrategyTest.WaitForHelloStartupCheckStrategy())) {5 container.start();6 }7 }8 public static class WaitForHelloStartupCheckStrategy implements StartupCheckStrategy {9 public boolean isReady(final GenericContainer container) {10 try {11 final String hello = container.execInContainer("echo", "hello").getStdout();12 return "hello".equals(hello.trim());13 } catch (final Exception e) {14 return false;15 }16 }17 }18}
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!!