Best Testcontainers-java code snippet using org.testcontainers.junit.FixedHostPortContainerTest
Source:FixedHostPortContainerTest.java
...8 * should be a field on the test class annotated with @Rule or @TestRule). Instead, here, the lifecycle of the container9 * is managed completely within the test method to allow a free port to be found and assigned before the container10 * is started.11 */12public class FixedHostPortContainerTest {13 private static final int REDIS_PORT = 6379;14 @Test15 public void testFixedHostPortMapping() throws IOException {16 // first find a free port on the docker host that will work for testing17 GenericContainer portDiscoveryRedis = new GenericContainer("redis:3.0.2").withExposedPorts(FixedHostPortContainerTest.REDIS_PORT);18 portDiscoveryRedis.start();19 Integer freePort = portDiscoveryRedis.getMappedPort(FixedHostPortContainerTest.REDIS_PORT);20 portDiscoveryRedis.stop();21 // Set up a FixedHostPortGenericContainer as if this were a @Rule22 FixedHostPortGenericContainer redis = new FixedHostPortGenericContainer("redis:3.0.2").withFixedExposedPort(freePort, FixedHostPortContainerTest.REDIS_PORT);23 redis.start();24 // Config redisConfig = new Config();25 // redisConfig.useSingleServer().setAddress(redis.getContainerIpAddress() + ":" + freePort);26 // Redisson redisson = Redisson.create(redisConfig);27 // 28 // redisson.getBucket("test").set("foo");29 // 30 // assertEquals("The bucket content was successfully set", "foo", redisson.getBucket("test").get());31 // assertEquals("The container returns the fixed port from getMappedPort(...)", freePort, redis.getMappedPort(REDIS_PORT));32 }33}...
FixedHostPortContainerTest
Using AI Code Generation
1import org.testcontainers.containers.FixedHostPortGenericContainer;2import org.testcontainers.containers.GenericContainer;3import org.testcontainers.containers.wait.strategy.Wait;4import org.testcontainers.utility.DockerImageName;5import org.junit.Rule;6import org.junit.Test;7import org.junit.rules.TestName;8import org.slf4j.Logger;9import org.slf4j.LoggerFactory;10import java.time.Duration;11import static org.junit.Assert.assertEquals;12public class FixedHostPortContainerTest {13 private static final Logger LOGGER = LoggerFactory.getLogger(FixedHostPortContainerTest.class);14 private static final String DOCKER_IMAGE_NAME = "nginx:1.21.0";15 private static final int HOST_PORT = 8080;16 private static final int CONTAINER_PORT = 80;17 private static final String EXPECTED_RESPONSE = "Welcome to nginx!";18 public TestName testName = new TestName();19 public GenericContainer<?> nginxContainer = new FixedHostPortGenericContainer<>(DockerImageName.parse(DOCKER_IMAGE_NAME))20 .withFixedExposedPort(HOST_PORT, CONTAINER_PORT)21 .waitingFor(Wait.forHttp("/").forStatusCode(200).withStartupTimeout(Duration.ofSeconds(10)));22 public void testNginxContainer() {23 LOGGER.info("Test name: {}", testName.getMethodName());24 String response = nginxContainer.execInContainer("curl", "localhost").getStdout();25 assertEquals(EXPECTED_RESPONSE, response);26 }27}28The FixedHostPortGenericContainer class is useful when you want to run a test container with a fixed host port. The FixedHostPortGenericContainer class also provides a withFixedExposedPort() method to map the container port to the host port. The withFixedExposedPort() method takes two arguments
FixedHostPortContainerTest
Using AI Code Generation
1import org.junit.ClassRule;2import org.junit.Test;3import org.openqa.selenium.remote.DesiredCapabilities;4import org.testcontainers.containers.BrowserWebDriverContainer;5import org.testcontainers.containers.FixedHostPortContainer;6import org.testcontainers.containers.GenericContainer;7import org.testcontainers.containers.wait.strategy.Wait;8import org.testcontainers.containers.wait.strategy.WaitAllStrategy;9public class TestContainerTest {10 public static FixedHostPortContainer seleniumContainer = new FixedHostPortContainer<>(11 new BrowserWebDriverContainer<>()12 .withDesiredCapabilities(DesiredCapabilities.chrome())13 .withRecordingMode(BrowserWebDriverContainer.VncRecordingMode.RECORD_ALL, new File("./build/"))14 ).withFixedExposedPort(4444, 4444);15 public void testContainer() {16 System.out.println(seleniumContainer.getContainerIpAddress());17 System.out.println(seleniumContainer.getMappedPort(4444));18 }19}
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!!