From bda181cafe9a651f3dfda8e1fdf062e4197f60a3 Mon Sep 17 00:00:00 2001 From: andy-stark-redis <164213578+andy-stark-redis@users.noreply.github.com> Date: Wed, 18 Sep 2024 14:24:57 +0100 Subject: [PATCH] DOC-4102 INCR command example (#3946) --- .../io/redis/examples/CmdsStringExample.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/test/java/io/redis/examples/CmdsStringExample.java diff --git a/src/test/java/io/redis/examples/CmdsStringExample.java b/src/test/java/io/redis/examples/CmdsStringExample.java new file mode 100644 index 0000000000..a1ba0281e5 --- /dev/null +++ b/src/test/java/io/redis/examples/CmdsStringExample.java @@ -0,0 +1,48 @@ +// EXAMPLE: cmds_string +// REMOVE_START +package io.redis.examples; + +import org.junit.Assert; +import org.junit.Test; + +// REMOVE_END +// HIDE_START +import redis.clients.jedis.UnifiedJedis; +// HIDE_END + +// HIDE_START +public class CmdsStringExample { + @Test + public void run() { + UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379"); + + //REMOVE_START + // Clear any keys here before using them in tests. + jedis.del("mykey"); + //REMOVE_END +// HIDE_END + + // STEP_START incr + String incrResult1 = jedis.set("mykey", "10"); + System.out.println(incrResult1); // >>> OK + + long incrResult2 = jedis.incr("mykey"); + System.out.println(incrResult2); // >>> 11 + + String incrResult3 = jedis.get("mykey"); + System.out.println(incrResult3); // >>> 11 + // STEP_END + + // Tests for 'incr' step. + // REMOVE_START + Assert.assertEquals("OK", incrResult1); + Assert.assertEquals(11, incrResult2); + Assert.assertEquals("11", incrResult3); + jedis.del("mykey"); + // REMOVE_END + +// HIDE_START + } +} +// HIDE_END +