Last active
August 31, 2020 09:46
-
-
Save darioblanco/688efd14e156ccb15f3a to your computer and use it in GitHub Desktop.
Mock a Jedis transaction with mockito
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import redis.clients.jedis.Client; | |
import redis.clients.jedis.Jedis; | |
import redis.clients.jedis.Transaction; | |
import org.testng.annotations.*; | |
import static org.mockito.Mockito.*; | |
public class TransactionMockFuckTest { | |
/** | |
* Let's say we want to test the following method | |
* public void run(Jedis jedis){ | |
* Transaction t = jedis.multi(); | |
* t.set("mykey", "myvalue"); | |
* t.expire("mykey", 100); | |
* t.exec(); | |
* } | |
*/ | |
@Test | |
public void testJedisTransaction(){ | |
String expectedKey = "mykey"; | |
String expectedValue = "myvalue"; | |
Jedis mockJedis = mock(Jedis.class); | |
Client mockClient = mock(Client.class); | |
Transaction spyTrans = spy(new Transaction(mockClient)); | |
when(mockJedis.multi()).thenReturn(spyTrans); | |
doNothing().when(mockClient).set(anyString(), anyString()); | |
MyClassToTest mc = new MyClassToTest(); | |
mc.run(mockJedis); | |
verify(mockClient, times(1)).set(expectedKey, expectedValue); | |
verify(mockClient, times(1)).expire(expectedKey, 100); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment