Best Testcontainers-java code snippet using org.testcontainers.junit.GenericContainerRuleTest
Source:GenericContainerRuleTest.java  
...29import org.testcontainers.utility.TestEnvironment;30/**31 * Tests for GenericContainerRules32 */33public class GenericContainerRuleTest {34    private static final int REDIS_PORT = 6379;35    private static final String RABBIQMQ_TEST_EXCHANGE = "TestExchange";36    private static final String RABBITMQ_TEST_ROUTING_KEY = "TestRoutingKey";37    private static final String RABBITMQ_TEST_MESSAGE = "Hello world";38    private static final int RABBITMQ_PORT = 5672;39    private static final int MONGO_PORT = 27017;40    /**41     * Redis42     */43    @ClassRule44    public static GenericContainer redis = new GenericContainer("redis:3.0.2").withExposedPorts(GenericContainerRuleTest.REDIS_PORT);45    /**46     * RabbitMQ47     */48    @ClassRule49    public static GenericContainer rabbitMq = new GenericContainer("rabbitmq:3.5.3").withExposedPorts(GenericContainerRuleTest.RABBITMQ_PORT);50    /**51     * MongoDB52     */53    @ClassRule54    public static GenericContainer mongo = new GenericContainer("mongo:3.1.5").withExposedPorts(GenericContainerRuleTest.MONGO_PORT);55    /**56     * Pass an environment variable to the container, then run a shell script that exposes the variable in a quick and57     * dirty way for testing.58     */59    @ClassRule60    public static GenericContainer alpineEnvVar = new GenericContainer("alpine:3.2").withExposedPorts(80).withEnv("MAGIC_NUMBER", "4").withEnv("MAGIC_NUMBER", ( oldValue) -> (oldValue.orElse("")) + "2").withCommand("/bin/sh", "-c", "while true; do echo \"$MAGIC_NUMBER\" | nc -l -p 80; done");61    /**62     * Pass environment variables to the container, then run a shell script that exposes the variables in a quick and63     * dirty way for testing.64     */65    @ClassRule66    public static GenericContainer alpineEnvVarFromMap = new GenericContainer("alpine:3.2").withExposedPorts(80).withEnv(ImmutableMap.of("FIRST", "42", "SECOND", "50")).withCommand("/bin/sh", "-c", "while true; do echo \"$FIRST and $SECOND\" | nc -l -p 80; done");67    /**68     * Map a file on the classpath to a file in the container, and then expose the content for testing.69     */70    @ClassRule71    public static GenericContainer alpineClasspathResource = new GenericContainer("alpine:3.2").withExposedPorts(80).withClasspathResourceMapping("mappable-resource/test-resource.txt", "/content.txt", BindMode.READ_ONLY).withCommand("/bin/sh", "-c", "while true; do cat /content.txt | nc -l -p 80; done");72    /**73     * Map a file on the classpath to a file in the container, and then expose the content for testing.74     */75    @ClassRule76    public static GenericContainer alpineClasspathResourceSelinux = new GenericContainer("alpine:3.2").withExposedPorts(80).withClasspathResourceMapping("mappable-resource/test-resource.txt", "/content.txt", BindMode.READ_WRITE, SelinuxContext.SHARED).withCommand("/bin/sh", "-c", "while true; do cat /content.txt | nc -l -p 80; done");77    /**78     * Create a container with an extra host entry and expose the content of /etc/hosts for testing.79     */80    @ClassRule81    public static GenericContainer alpineExtrahost = new GenericContainer("alpine:3.2").withExposedPorts(80).withExtraHost("somehost", "192.168.1.10").withCommand("/bin/sh", "-c", "while true; do cat /etc/hosts | nc -l -p 80; done");82    // @Test83    // public void simpleRedisTest() {84    // String ipAddress = redis.getContainerIpAddress();85    // Integer port = redis.getMappedPort(REDIS_PORT);86    // 87    // // Use Redisson to obtain a List that is backed by Redis88    // Config redisConfig = new Config();89    // redisConfig.useSingleServer().setAddress(ipAddress + ":" + port);90    // 91    // Redisson redisson = Redisson.create(redisConfig);92    // 93    // List<String> testList = redisson.getList("test");94    // testList.add("foo");95    // testList.add("bar");96    // testList.add("baz");97    // 98    // List<String> testList2 = redisson.getList("test");99    // assertEquals("The list contains the expected number of items (redis is working!)", 3, testList2.size());100    // assertTrue("The list contains an item that was put in (redis is working!)", testList2.contains("foo"));101    // assertTrue("The list contains an item that was put in (redis is working!)", testList2.contains("bar"));102    // assertTrue("The list contains an item that was put in (redis is working!)", testList2.contains("baz"));103    // }104    @Test105    public void testIsRunning() {106        try (GenericContainer container = new GenericContainer().withCommand("top")) {107            assertFalse("Container is not started and not running", container.isRunning());108            container.start();109            assertTrue("Container is started and running", container.isRunning());110        }111    }112    @Test113    public void withTmpFsTest() throws Exception {114        try (GenericContainer container = new GenericContainer().withCommand("top").withTmpFs(Collections.singletonMap("/testtmpfs", "rw"))) {115            container.start();116            // check file doesn't exist117            String path = "/testtmpfs/test.file";118            Container.ExecResult execResult = container.execInContainer("ls", path);119            assertEquals("tmpfs inside container works fine", execResult.getStderr(), "ls: /testtmpfs/test.file: No such file or directory\n");120            // touch && check file does exist121            container.execInContainer("touch", path);122            execResult = container.execInContainer("ls", path);123            assertEquals("tmpfs inside container works fine", execResult.getStdout(), (path + "\n"));124        }125    }126    @Test127    public void simpleRabbitMqTest() throws IOException, TimeoutException {128        ConnectionFactory factory = new ConnectionFactory();129        factory.setHost(GenericContainerRuleTest.rabbitMq.getContainerIpAddress());130        factory.setPort(GenericContainerRuleTest.rabbitMq.getMappedPort(GenericContainerRuleTest.RABBITMQ_PORT));131        Connection connection = factory.newConnection();132        Channel channel = connection.createChannel();133        channel.exchangeDeclare(GenericContainerRuleTest.RABBIQMQ_TEST_EXCHANGE, "direct", true);134        String queueName = channel.queueDeclare().getQueue();135        channel.queueBind(queueName, GenericContainerRuleTest.RABBIQMQ_TEST_EXCHANGE, GenericContainerRuleTest.RABBITMQ_TEST_ROUTING_KEY);136        // Set up a consumer on the queue137        final boolean[] messageWasReceived = new boolean[1];138        channel.basicConsume(queueName, false, new DefaultConsumer(channel) {139            @Override140            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {141                messageWasReceived[0] = Arrays.equals(body, GenericContainerRuleTest.RABBITMQ_TEST_MESSAGE.getBytes());142            }143        });144        // post a message145        channel.basicPublish(GenericContainerRuleTest.RABBIQMQ_TEST_EXCHANGE, GenericContainerRuleTest.RABBITMQ_TEST_ROUTING_KEY, null, GenericContainerRuleTest.RABBITMQ_TEST_MESSAGE.getBytes());146        // check the message was received147        assertTrue("The message was received", Unreliables.retryUntilSuccess(5, TimeUnit.SECONDS, () -> {148            if (!(messageWasReceived[0])) {149                throw new IllegalStateException("Message not received yet");150            }151            return true;152        }));153    }154    @Test155    public void simpleMongoDbTest() {156        MongoClient mongoClient = new MongoClient(GenericContainerRuleTest.mongo.getContainerIpAddress(), GenericContainerRuleTest.mongo.getMappedPort(GenericContainerRuleTest.MONGO_PORT));157        MongoDatabase database = mongoClient.getDatabase("test");158        MongoCollection<Document> collection = database.getCollection("testCollection");159        Document doc = new Document("name", "foo").append("value", 1);160        collection.insertOne(doc);161        Document doc2 = collection.find(new Document("name", "foo")).first();162        assertEquals("A record can be inserted into and retrieved from MongoDB", 1, doc2.get("value"));163    }164    @Test165    public void environmentAndCustomCommandTest() throws IOException {166        String line = getReaderForContainerPort80(GenericContainerRuleTest.alpineEnvVar).readLine();167        assertEquals("An environment variable can be passed into a command", "42", line);168    }169    @Test170    public void environmentFromMapTest() throws IOException {171        String line = getReaderForContainerPort80(GenericContainerRuleTest.alpineEnvVarFromMap).readLine();172        assertEquals("Environment variables can be passed into a command from a map", "42 and 50", line);173    }174    @Test175    public void customLabelTest() {176        try (final GenericContainer alpineCustomLabel = new GenericContainer("alpine:3.2").withLabel("our.custom", "label").withCommand("top")) {177            alpineCustomLabel.start();178            Map<String, String> labels = alpineCustomLabel.getCurrentContainerInfo().getConfig().getLabels();179            assertTrue("org.testcontainers label is present", labels.containsKey("org.testcontainers"));180            assertTrue("our.custom label is present", labels.containsKey("our.custom"));181            assertEquals("our.custom label value is label", labels.get("our.custom"), "label");182        }183    }184    @Test185    public void exceptionThrownWhenTryingToOverrideTestcontainersLabels() {186        assertThrows("When trying to overwrite an 'org.testcontainers' label, withLabel() throws an exception", IllegalArgumentException.class, () -> {187            new GenericContainer("alpine:3.2").withLabel("org.testcontainers.foo", "false");188        });189    }190    @Test191    public void customClasspathResourceMappingTest() throws IOException {192        // Note: This functionality doesn't work if you are running your build inside a Docker container;193        // in that case this test will fail.194        String line = getReaderForContainerPort80(GenericContainerRuleTest.alpineClasspathResource).readLine();195        assertEquals("Resource on the classpath can be mapped using calls to withClasspathResourceMapping", "FOOBAR", line);196    }197    @Test198    public void customClasspathResourceMappingWithSelinuxTest() throws IOException {199        String line = getReaderForContainerPort80(GenericContainerRuleTest.alpineClasspathResourceSelinux).readLine();200        assertEquals("Resource on the classpath can be mapped using calls to withClasspathResourceMappingSelinux", "FOOBAR", line);201    }202    @Test203    public void exceptionThrownWhenMappedPortNotFound() throws IOException {204        assertThrows("When the requested port is not mapped, getMappedPort() throws an exception", IllegalArgumentException.class, () -> {205            return GenericContainerRuleTest.redis.getMappedPort(666);206        });207    }208    @Test209    public void testExecInContainer() throws Exception {210        // The older "lxc" execution driver doesn't support "exec". At the time of writing (2016/03/29),211        // that's the case for CircleCI.212        // Once they resolve the issue, this clause can be removed.213        Assume.assumeTrue(TestEnvironment.dockerExecutionDriverSupportsExec());214        final GenericContainer.ExecResult result = GenericContainerRuleTest.redis.execInContainer("redis-cli", "role");215        assertTrue("Output for \"redis-cli role\" command should start with \"master\"", result.getStdout().startsWith("master"));216        assertEquals("Stderr for \"redis-cli role\" command should be empty", "", result.getStderr());217        // We expect to reach this point for modern Docker versions.218    }219    @Test220    public void extraHostTest() throws IOException {221        BufferedReader br = getReaderForContainerPort80(GenericContainerRuleTest.alpineExtrahost);222        // read hosts file from container223        StringBuffer hosts = new StringBuffer();224        String line = br.readLine();225        while (line != null) {226            hosts.append(line);227            hosts.append("\n");228            line = br.readLine();229        } 230        Matcher matcher = Pattern.compile("^192.168.1.10\\s.*somehost", Pattern.MULTILINE).matcher(hosts.toString());231        assertTrue("The hosts file of container contains extra host", matcher.find());232    }233    @Test234    public void createContainerCmdHookTest() {235        // Use random name to avoid the conflicts between the tests236        String randomName = Base58.randomString(5);237        try (GenericContainer container = // Allows to override pre-configured values by GenericContainer238        // Preserves the order239        new GenericContainer("redis:3.0.2").withCommand("redis-server", "--help").withCreateContainerCmdModifier(( cmd) -> cmd.withName("overrideMe")).withCreateContainerCmdModifier(( cmd) -> cmd.withName(randomName)).withCreateContainerCmdModifier(( cmd) -> cmd.withCmd("redis-server", "--port", "6379"))) {240            container.start();241            assertEquals("Name is configured", ("/" + randomName), container.getContainerInfo().getName());242            assertEquals("Command is configured", "[redis-server, --port, 6379]", Arrays.toString(container.getContainerInfo().getConfig().getCmd()));243        }244    }245    @Test246    public void addExposedPortAfterWithExposedPortsTest() {247        GenericContainerRuleTest.redis.addExposedPort(8987);248        assertThat("Both ports should be exposed", GenericContainerRuleTest.redis.getExposedPorts().size(), CoreMatchers.equalTo(2));249        assertTrue("withExposedPort should be exposed", GenericContainerRuleTest.redis.getExposedPorts().contains(GenericContainerRuleTest.REDIS_PORT));250        assertTrue("addExposedPort should be exposed", GenericContainerRuleTest.redis.getExposedPorts().contains(8987));251    }252    @Test253    public void sharedMemorySetTest() {254        try (GenericContainer containerWithSharedMemory = new GenericContainer("busybox:1.29").withSharedMemorySize((1024L * (FileUtils.ONE_MB)))) {255            containerWithSharedMemory.start();256            HostConfig hostConfig = containerWithSharedMemory.getDockerClient().inspectContainerCmd(containerWithSharedMemory.getContainerId()).exec().getHostConfig();257            assertEquals("Shared memory not set on container", hostConfig.getShmSize(), (1024 * (FileUtils.ONE_MB)));258        }259    }260}...GenericContainerRuleTest
Using AI Code Generation
1import org.junit.ClassRule;2import org.testcontainers.containers.GenericContainer;3import org.testcontainers.junit.GenericContainerRuleTest;4public class GenericContainerRuleTest {5public static GenericContainer redis = new GenericContainer("redis:3.0.2")6.withExposedPorts(6379);7}8}GenericContainerRuleTest
Using AI Code Generation
1        public GenericContainerRuleTest redis = new GenericContainerRuleTest(new ImageFromDockerfile()2                .withDockerfileFromBuilder(builder -> builder3                        .from("redis:3.2.6")4                        .build()));5        public void testSimple() throws Exception {6            try (Jedis jedis = new Jedis(redis.getContainerIpAddress(), redis.getFirstMappedPort())) {7                jedis.set("foo", "bar");8                assertThat(jedis.get("foo"), is("bar"));9            }10        }11    }12    @RunWith(Theories.class)13    public class GenericContainerRuleTheoryTest {14        public GenericContainerRuleTest redis = new GenericContainerRuleTest(new ImageFromDockerfile()15                .withDockerfileFromBuilder(builder -> builder16                        .from("redis:3.2.6")17                        .build()));18        public static String[] data() {19            return new String[] {"foo", "bar"};20        }21        public void testTheory(String data) throws Exception {22            try (Jedis jedis = new Jedis(redis.getContainerIpAddress(), redis.getFirstMappedPort())) {23                jedis.set(data, data);24                assertThat(jedis.get(data), is(data));25            }26        }27    }28    public class GenericContainerRuleTest extends GenericContainer<GenericContainerRuleTest> {29        public GenericContainerRuleTest(ImageFromDockerfile image) {30            super(image);31        }32    }33}GenericContainerRuleTest
Using AI Code Generation
1import org.junit.jupiter.api.Test;2import org.testcontainers.containers.GenericContainer;3public class GenericContainerRuleTest {4    public void testGenericContainer() {5        try (GenericContainer container = new GenericContainer("alpine:3.5")6                .withCommand("ping", "GenericContainerRuleTest
Using AI Code Generation
1    public GenericContainerRuleTest container = new GenericContainerRuleTest("couchbase/server:6.0.2")2            .withExposedPorts(8091, 8092, 8093, 8094, 11210, 11207, 18091, 18092, 18093, 18094, 18095, 18096, 18097, 18098, 18099)3            .withEnv("COUCHBASE_MEMORY_QUOTA_MB", "256")4            .withEnv("COUCHBASE_INDEX_MEMORY_QUOTA_MB", "256")5            .withEnv("COUCHBASE_CLUSTER_RAMSIZE_MB", "256")6            .withEnv("COUCHBASE_CLUSTER_INDEX_RAMSIZE_MB", "256")7            .withEnv("COUCHBASE_EVENTING_MEMORY_QUOTA_MB", "256")8            .withEnv("COUCHBASE_FTS_MEMORY_QUOTA_MB", "256")9            .withEnv("COUCHBASE_ANALYTICS_MEMORY_QUOTA_MB", "256")10            .withEnv("COUCHBASE_AUTO_REPROVISION", "true")11            .withEnv("COUCHBASE_AUTOFAIL_OVER_SERVERS", "0")12            .withEnv("COUCHBASE_INDEX_STORAGE_MODE", "memory_optimized")13            .withEnv("COUCHBASE_MAX_NUM_ROLLBACKS", "10")14            .withEnv("COUCHBASE_MAX_NUM_INDEX_ROLLBACKS", "10")15            .withEnv("COUCHBASE_NUM_INDEXER_REPLICAS", "0")16            .withEnv("COUCHBASE_NUM_INDEXER_PARTITIONS", "8")17            .withEnv("COUCHBASE_NUM_KV_REPLICAS", "0")18            .withEnv("COUCHBASE_NUM_EVENTING_REPLICAS", "0")19            .withEnv("COUCHBASE_NUM_FTS_REPLICAS", "0")20            .withEnv("COUCHBASE_NUM_ANALYTICS_REPLICAS", "0")21            .withEnv("COUCHBASE_NUM_INDEXER_THREADS", "0")22            .withEnv("COUCHBASE_NUM_EVENTING_THREADS", "0")23            .withEnv("COUCHBASE_NUM_FTS_THREADS", "0")24            .withEnv("COUCHBASE_NUM_ANALYTICS_THREADS", "0")25            .withEnv("COUCHLearn 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!!
