• Home
  • LLMs
  • Python
  • Docker
  • Kubernetes
  • Java
  • Maven
  • All
  • About
Testing | Using ZooKeeper Curator framework (TestingCluster)
  1. The POM file (pom.xml)
  2. Unit test example

  1. The POM file (pom.xml)
    <project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>mtitek.zk.embed.samples</groupId>
        <artifactId>mtitek-zk-embed-samples</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    
            <maven.compiler.source>23</maven.compiler.source> <!-- maven-compiler-plugin property -->
            <maven.compiler.target>23</maven.compiler.target> <!-- maven-compiler-plugin property -->
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-framework</artifactId>
                <version>5.8.0</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-test</artifactId>
                <version>5.8.0</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-client</artifactId>
                <version>5.8.0</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.13.2</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.14.0</version>
    
                    <configuration>
                        <encoding>UTF-8</encoding>
                        <source>${maven.compiler.source}</source> <!-- optional if maven.compiler.source property is set -->
                        <target>${maven.compiler.target}</target> <!-- optional if maven.compiler.target property is set -->
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
  2. Unit test example
    mtitek-zk-embed-samples/src/test/java/mtitek/zk/embed/samples/TestingClusterTest.java:
    package mtitek.zk.embed.samples;
    
    import org.apache.curator.framework.CuratorFramework;
    import org.apache.curator.framework.CuratorFrameworkFactory;
    import org.apache.curator.retry.RetryOneTime;
    import org.apache.curator.test.TestingCluster;
    import org.junit.Test;
    
    public class TestingClusterTest {
        @Test
        public void test() throws Exception {
            final int instanceQty = 1;
    
            // create TestingCluster instance
            final TestingCluster testingCluster = new TestingCluster(instanceQty);
    
            // start TestingCluster
            {
                testingCluster.start();
            }
    
            // configure CuratorFramework
            {
                final int sessionTimeoutMs = Integer.valueOf(60 * 1000);
                final int connectionTimeoutMs = Integer.valueOf(15 * 1000);
                final int sleepMsBetweenRetry = Integer.valueOf(1 * 1000);
    
                final String connectString = testingCluster.getConnectString();
    
                // create CuratorFramework
                CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(connectString, sessionTimeoutMs, connectionTimeoutMs, new RetryOneTime(sleepMsBetweenRetry));
    
                // start CuratorFramework
                curatorFramework.start();
    
                // check the state of the CuratorFramework instance
                System.out.println("CuratorFramework State: " + curatorFramework.getState());
    
                // close CuratorFramework
                curatorFramework.close();
            }
    
            // stop TestingCluster
            {
                testingCluster.close();
            }
        }
    }
© 2025  mtitek