The need for it
[wp_ad_camp_5]
Finding out if an object is a mock or spy in Mockito may not (or never) be part of your usual automated unit tests but in some cases (advanced usage of JUnit/Mockito) you may need to determine its type.
Is Mock or Spy?
Below is a sample code to determine if an object is mock or spy in Mockito.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | @Test public void test_dependencies_mock_or_spy() { // dependency1 is a mock object Assert.assertTrue(Mockito.mockingDetails(dependency1).isMock()); // dependency1 is not a spy object Assert.assertFalse(Mockito.mockingDetails(dependency1).isSpy()); // dependency2 is not a mock object Assert.assertTrue(Mockito.mockingDetails(dependency2).isMock()); // dependency2 is a spy object Assert.assertTrue(Mockito.mockingDetails(dependency2).isSpy()); } |
Download the Codes
https://github.com/Turreta/mockito-spy-or-mock-example
[wp_ad_camp_1]