Skip to content

Instantly share code, notes, and snippets.

@alainlompo
Created February 14, 2017 02:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alainlompo/39887084c6dcbe123ea01a7e9e308557 to your computer and use it in GitHub Desktop.
Save alainlompo/39887084c6dcbe123ea01a7e9e308557 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