Skip to content

Instantly share code, notes, and snippets.

@alpizano
Forked from alainlompo/MockitoSpyExample.java
Created August 15, 2022 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alpizano/704f1446136477f37a75bd3ee5af611e to your computer and use it in GitHub Desktop.
Save alpizano/704f1446136477f37a75bd3ee5af611e to your computer and use it in GitHub Desktop.
Mockito spy example
import static org.mockito.Mockito.*;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
public class MockitoSpyExample {
@Test
public void spyExample_nominal_case_Test() {
Map<String, String> hashMap = new HashMap<String, String>();
Map<String, String> mapSpy = spy(hashMap);
System.out.println(mapSpy.get("key"));
mapSpy.put("key", "Test value");
System.out.println(mapSpy.get("key")); // Will print "Test value".
when(mapSpy.get("key")).thenReturn("Mocked value");
System.out.println(mapSpy.get("key")); // Will print "Mocked vqlue".
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment