DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Steps To Integrate Selenium Test Case With Apache JMeter
  • Mock API KIT: Router and Write Munit Test Cases for Different Error Types
  • Hybrid Vector Graph with AI Agents for Software Test Case Creation
  • Automating FastAPI Deployments With a GitHub Actions Pipeline

Trending

  • Mocking Kafka for Local Spring Development
  • Introduction to Tactical DDD With Java: Steps to Build Semantic Code
  • Rethinking Java CRUDs With Event Sourcing and CQRS Patterns
  • Lambda-Driven API Design: Building Composable Node.js Endpoints With Functional Primitives
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Testing Jedis API Using Junit Test Case in Eclipse IDE

Testing Jedis API Using Junit Test Case in Eclipse IDE

In this article, we will learn how to set up a Junit testing environment for testing Jedis API, which will interact with the Redis database.

By 
Tiruvenkatasamy Baskaran user avatar
Tiruvenkatasamy Baskaran
·
Nov. 04, 20 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
8.1K Views

Join the DZone community and get the full member experience.

Join For Free

Prerequisites

  • Eclipse (any version) with Maven capabilities
  • Java 8+
  • Junit
  • Redis and Jedis

Installing Redis Server on Windows   

  1. Click on the link: https://github.com/dmajkic/redis/downloads
  2. Download redis-2.4.5-win32-win64.zip file
  3. Unzip the file and go to the 64bit folder. There you can find redis-server.exe
  4. To start the Redis server, execute the redis-server.exe file.

Installing Eclipse-IDE on Windows    

    1.Click on the link: https://www.eclipse.org/downloads/download.php?file=/oomph/epp/2020-09/R/eclipse-inst-jre-win64.exe

    2. Download eclipse-inst-jre-win64.exe file and run the eclipse installer.

         

     3.Select Eclipse IDE for Eclipse committers and install

           

Creating Maven Project in Eclipse IDE

  1.Open the Eclipse IDE

    2.Go to File  > New > Project      

    3.Go to Maven -> Maven Project and click Next.  

    4.Select your workspace location and click Next

       

      5.Select quick start maven archetype and click Next.

        

      6.Enter Group Id, Artifact Id, and package name.

              

  • Group Id: Fill a groupId for the project of your choice.
  • Artifact Id: Fill artifactId for the project of your choice.
  • Package: java package structure of your choice

     7.The above process will create a project structure like below.

     

   

      8.Place the HostPort.java file in com.example.demo package.            

Java
 




x
20


 
1
package com.example.demo;
2

          
3
public class HostPort {
4
    final private static String defaultHost = "localhost";
5
    final private static Integer defaultPort = 6379;
6
    final private static String defaultPassword = "";
7

          
8
    public static String getRedisHost() {
9
        return defaultHost;
10
    }
11

          
12
    public static Integer getRedisPort() {
13
        return defaultPort;
14
    }
15

          
16
    public static String getRedisPassword() {
17
        return defaultPassword;
18
    }
19
}
20

          


       9.Add the WelcomeTest.java file in com.example.demo package.

    

Java
 




xxxxxxxxxx
1
72


 
1
package com.example.demo;
2

          
3
import org.junit.Test;
4

          
5
import com.example.demo.HostPort;
6

          
7
import redis.clients.jedis.Jedis;
8
import redis.clients.jedis.JedisPool;
9
import redis.clients.jedis.JedisPoolConfig;
10

          
11
import static org.hamcrest.MatcherAssert.assertThat;
12
import static org.hamcrest.Matchers.is;
13

          
14
public class WelcomeTest {
15

          
16
    @Test
17
    public void sayWelcomeBasic() {
18
        Jedis jedis = new Jedis(HostPort.getRedisHost(), HostPort.getRedisPort());
19
        
20
        System.out.print("Host"+HostPort.getRedisHost());
21

          
22
        if (HostPort.getRedisPassword().length() > 0) {
23
            jedis.auth(HostPort.getRedisPassword());
24
        }
25

          
26
        jedis.set("welcome", "world");
27
        String value = jedis.get("welcome");
28

          
29
        assertThat(value, is("world"));
30
    }
31

          
32
    @Test
33
    public void sayWelcome() {
34
        Jedis jedis = new Jedis(HostPort.getRedisHost(), HostPort.getRedisPort());
35

          
36
        if (HostPort.getRedisPassword().length() > 0) {
37
            jedis.auth(HostPort.getRedisPassword());
38
        }
39

          
40
        String result = jedis.set("welcome", "world");
41
        assertThat(result, is("OK"));
42
        String value = jedis.get("welcome");
43
        assertThat(value, is("world"));
44

          
45
        jedis.close();
46
    }
47

          
48
    @Test
49
    public void sayWelcomeThreadSafe() {
50
        JedisPool jedisPool;
51

          
52
        String password = HostPort.getRedisPassword();
53

          
54
       if (password.length() > 0) {
55
           jedisPool = new JedisPool(new JedisPoolConfig(),
56
                HostPort.getRedisHost(), HostPort.getRedisPort(), 2000, password);
57
        } else {
58
            jedisPool = new JedisPool(new JedisPoolConfig(),
59
                HostPort.getRedisHost(), HostPort.getRedisPort());
60
        }
61

          
62
        try (Jedis jedis = jedisPool.getResource()) {
63
            String result = jedis.set("welcome", "world");
64
            assertThat(result, is("OK"));
65
            String value = jedis.get("welcome");
66
            assertThat(value, is("world"));
67
        }
68

          
69
        jedisPool.close();
70
    }
71
}
72

          



10. Replace the pom.xml with the below content.   

XML
 
xxxxxxxxxx
1
212


 
1
<?xml version="1.0" encoding="UTF-8"?>
2
<project
3
        xmlns="http://maven.apache.org/POM/4.0.0"
4
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
6

          
7
    <modelVersion>4.0.0</modelVersion>
8
    <prerequisites>
9
        <maven>3.0.0</maven>
10
    </prerequisites>
11

          
12
    <groupId>JedisJunitDemo</groupId>
13
    <artifactId>JedisJunitDemo</artifactId>
14
    <version>1.0</version>
15
    <packaging>jar</packaging>
16

          
17
    <name>JedisJunitDemo</name>
18

          
19
    <properties>
20
        <redis.host>localhost</redis.host>
21
        <redis.port>6379</redis.port>
22
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24
        <dropwizard.version>1.3.8</dropwizard.version>
25
        <mainClass>com.example.demo.RediSolarApplication</mainClass>
26
    </properties>
27

          
28
    <dependencyManagement>
29
        <dependencies>
30
            <dependency>
31
                <groupId>io.dropwizard</groupId>
32
                <artifactId>dropwizard-bom</artifactId>
33
                <version>${dropwizard.version}</version>
34
                <type>pom</type>
35
                <scope>import</scope>
36
            </dependency>
37
        </dependencies>
38
    </dependencyManagement>
39

          
40
    <dependencies>
41
        <dependency>
42
            <groupId>com.google.inject</groupId>
43
            <artifactId>guice</artifactId>
44
            <version>4.2.2</version>
45
        </dependency>
46
        <dependency>
47
            <groupId>io.dropwizard</groupId>
48
            <artifactId>dropwizard-core</artifactId>
49
        </dependency>
50
        <dependency>
51
            <groupId>io.dropwizard</groupId>
52
            <artifactId>dropwizard-assets</artifactId>
53
        </dependency>
54
        <dependency>
55
            <groupId>io.dropwizard</groupId>
56
            <artifactId>dropwizard-testing</artifactId>
57
        </dependency>
58
        <dependency>
59
            <groupId>redis.clients</groupId>
60
            <artifactId>jedis</artifactId>
61
            <version>3.1.0-m3</version>
62
        </dependency>
63
        <dependency>
64
            <groupId>com.redislabs</groupId>
65
            <artifactId>jredistimeseries</artifactId>
66
            <version>0.9.0</version>
67
        </dependency>
68
        <dependency>
69
            <groupId>javax.xml.bind</groupId>
70
            <artifactId>jaxb-api</artifactId>
71
            <version>2.2.11</version>
72
        </dependency>
73
        <dependency>
74
            <groupId>com.sun.xml.bind</groupId>
75
            <artifactId>jaxb-core</artifactId>
76
            <version>2.2.11</version>
77
        </dependency>
78
        <dependency>
79
            <groupId>com.sun.xml.bind</groupId>
80
            <artifactId>jaxb-impl</artifactId>
81
            <version>2.2.11</version>
82
        </dependency>
83
        <dependency>
84
            <groupId>javax.activation</groupId>
85
            <artifactId>activation</artifactId>
86
            <version>1.1.1</version>
87
        </dependency>
88
        <dependency>
89
            <groupId>junit</groupId>
90
            <artifactId>junit</artifactId>
91
            <version>4.12</version>
92
            <scope>test</scope>
93
        </dependency>
94
        <dependency>
95
            <groupId>org.hamcrest</groupId>
96
            <artifactId>hamcrest-all</artifactId>
97
            <version>1.3</version>
98
            <scope>test</scope>
99
        </dependency>
100
        <dependency>
101
            <groupId>org.mockito</groupId>
102
            <artifactId>mockito-all</artifactId>
103
            <version>1.9.5</version>
104
            <scope>test</scope>
105
        </dependency>
106
    </dependencies>
107

          
108
    <build>
109
        <plugins>
110
            <plugin>
111
                <groupId>org.codehaus.mojo</groupId>
112
                <artifactId>properties-maven-plugin</artifactId>
113
                <version>1.0.0</version>
114
                <executions>
115
                    <execution>
116
                        <phase>initialize</phase>
117
                        <goals>
118
                            <goal>read-project-properties</goal>
119
                        </goals>
120
                        <configuration>
121
                            <files>
122
                                <file>pom.xml</file>
123
                            </files>
124
                        </configuration>
125
                    </execution>
126
                </executions>
127
            </plugin>
128
            <plugin>
129
                <artifactId>maven-shade-plugin</artifactId>
130
                <version>2.4.1</version>
131
                <configuration>
132
                    <createDependencyReducedPom>false</createDependencyReducedPom>
133
                    <transformers>
134
                        <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
135
                        <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
136
                            <mainClass>${mainClass}</mainClass>
137
                        </transformer>
138
                    </transformers>
139
                    <!-- exclude signed Manifests -->
140
                    <filters>
141
                        <filter>
142
                            <artifact>*:*</artifact>
143
                            <excludes>
144
                                <exclude>META-INF/*.SF</exclude>
145
                                <exclude>META-INF/*.DSA</exclude>
146
                                <exclude>META-INF/*.RSA</exclude>
147
                            </excludes>
148
                        </filter>
149
                    </filters>
150
                </configuration>
151
                <executions>
152
                    <execution>
153
                        <phase>package</phase>
154
                        <goals>
155
                            <goal>shade</goal>
156
                        </goals>
157
                    </execution>
158
                </executions>
159
            </plugin>
160
            <plugin>
161
                <artifactId>maven-jar-plugin</artifactId>
162
                <version>3.0.2</version>
163
                <configuration>
164
                    <archive>
165
                        <manifest>
166
                            <addClasspath>true</addClasspath>
167
                            <mainClass>${mainClass}</mainClass>
168
                        </manifest>
169
                    </archive>
170
                </configuration>
171
            </plugin>
172
            <plugin>
173
                <artifactId>maven-compiler-plugin</artifactId>
174
                <version>3.6.1</version>
175
                <configuration>
176
                    <source>1.8</source>
177
                    <target>1.8</target>
178
                </configuration>
179
            </plugin>
180
            <plugin>
181
                <artifactId>maven-source-plugin</artifactId>
182
                <version>3.0.1</version>
183
                <executions>
184
                    <execution>
185
                        <id>attach-sources</id>
186
                        <goals>
187
                            <goal>jar</goal>
188
                        </goals>
189
                    </execution>
190
                </executions>
191
            </plugin>
192
        </plugins>
193
    </build>
194

          
195
    <reporting>
196
        <plugins>
197
            <plugin>
198
                <artifactId>maven-project-info-reports-plugin</artifactId>
199
                <version>2.8.1</version>
200
                <configuration>
201
                    <dependencyLocationsEnabled>false</dependencyLocationsEnabled>
202
                    <dependencyDetailsEnabled>false</dependencyDetailsEnabled>
203
                </configuration>
204
            </plugin>
205
            <plugin>
206
                <artifactId>maven-javadoc-plugin</artifactId>
207
                <version>2.10.3</version>
208
            </plugin>
209
        </plugins>
210
    </reporting>
211
</project>
212

          



Running the Junit Test Case     

  1. Build the Maven project and run the test case as shown below.

       2.Check the test result.

                                                                


Cross Verifying the Result From Redis Client

       

  1. To start the Redis client, execute the redis-cli.exe file
  2. Type get welcome on the console, I will give world 

                         

So, we are able to test the Jedis API using Junit.

Further, this test case can be customized according to your needs.

Feel free to ask questions.

Integrated development environment Test case API Eclipse Testing JUnit Jedi (game engine)

Opinions expressed by DZone contributors are their own.

Related

  • Steps To Integrate Selenium Test Case With Apache JMeter
  • Mock API KIT: Router and Write Munit Test Cases for Different Error Types
  • Hybrid Vector Graph with AI Agents for Software Test Case Creation
  • Automating FastAPI Deployments With a GitHub Actions Pipeline

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook