Best Mockito code snippet using org.mockitousage.spies.SpyingOnRealObjectsTest.shouldVerify
Source:SpyingOnRealObjectsTest.java  
...20public class SpyingOnRealObjectsTest extends TestBase {21    List<String> list = new LinkedList<String>();22    List<String> spy = Mockito.spy(list);23    @Test24    public void shouldVerify() {25        spy.add("one");26        spy.add("two");27        Assert.assertEquals("one", spy.get(0));28        Assert.assertEquals("two", spy.get(1));29        Mockito.verify(spy).add("one");30        Mockito.verify(spy).add("two");31    }32    @SuppressWarnings({ "CheckReturnValue", "MockitoUsage" })33    @Test34    public void shouldBeAbleToMockObjectBecauseWhyNot() {35        Mockito.spy(new Object());36    }37    @Test38    public void shouldStub() {39        spy.add("one");40        Mockito.when(spy.get(0)).thenReturn("1").thenReturn("1 again");41        Assert.assertEquals("1", spy.get(0));42        Assert.assertEquals("1 again", spy.get(0));43        Assert.assertEquals("one", spy.iterator().next());44        Assert.assertEquals(1, spy.size());45    }46    @Test47    public void shouldAllowOverridingStubs() {48        Mockito.when(spy.contains(ArgumentMatchers.anyObject())).thenReturn(true);49        Mockito.when(spy.contains("foo")).thenReturn(false);50        Assert.assertTrue(spy.contains("bar"));51        Assert.assertFalse(spy.contains("foo"));52    }53    @Test54    public void shouldStubVoid() {55        Mockito.doNothing().doThrow(new RuntimeException()).when(spy).clear();56        spy.add("one");57        spy.clear();58        try {59            spy.clear();60            Assert.fail();61        } catch (RuntimeException e) {62        }63        Assert.assertEquals(1, spy.size());64    }65    @Test66    public void shouldStubWithDoReturnAndVerify() {67        Mockito.doReturn("foo").doReturn("bar").when(spy).get(0);68        Assert.assertEquals("foo", spy.get(0));69        Assert.assertEquals("bar", spy.get(0));70        Mockito.verify(spy, Mockito.times(2)).get(0);71        Mockito.verifyNoMoreInteractions(spy);72    }73    @Test74    public void shouldVerifyInOrder() {75        spy.add("one");76        spy.add("two");77        InOrder inOrder = Mockito.inOrder(spy);78        inOrder.verify(spy).add("one");79        inOrder.verify(spy).add("two");80        Mockito.verifyNoMoreInteractions(spy);81    }82    @Test83    public void shouldVerifyInOrderAndFail() {84        spy.add("one");85        spy.add("two");86        InOrder inOrder = Mockito.inOrder(spy);87        inOrder.verify(spy).add("two");88        try {89            inOrder.verify(spy).add("one");90            Assert.fail();91        } catch (VerificationInOrderFailure f) {92        }93    }94    @Test95    public void shouldVerifyNumberOfTimes() {96        spy.add("one");97        spy.add("one");98        Mockito.verify(spy, Mockito.times(2)).add("one");99        Mockito.verifyNoMoreInteractions(spy);100    }101    @Test102    public void shouldVerifyNumberOfTimesAndFail() {103        spy.add("one");104        spy.add("one");105        try {106            Mockito.verify(spy, Mockito.times(3)).add("one");107            Assert.fail();108        } catch (TooLittleActualInvocations e) {109        }110    }111    @Test112    public void shouldVerifyNoMoreInteractionsAndFail() {113        spy.add("one");114        spy.add("two");115        Mockito.verify(spy).add("one");116        try {117            Mockito.verifyNoMoreInteractions(spy);118            Assert.fail();119        } catch (NoInteractionsWanted e) {120        }121    }122    @Test123    public void shouldToString() {124        spy.add("foo");125        Assert.assertEquals("[foo]", spy.toString());126    }...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
