Best Mockito code snippet using org.mockitousage.verification.VerificationWithAfterTest.call
Source:VerificationWithAfterTest.java
...21 @Rule22 public MockitoRule mockito = MockitoJUnit.rule();23 @Mock24 private IMethods mock;25 private Runnable callMock = new Runnable() {26 public void run() {27 mock.oneArg('1');28 }29 };30 private AsyncTesting async = new AsyncTesting();31 private Stopwatch watch = Stopwatch.createNotStarted();32 @Test33 public void should_verify_with_after() {34 // given35 async.runAfter(10, callMock);36 async.runAfter(1000, callMock);37 // then38 Mockito.verify(mock, Mockito.after(300)).oneArg('1');39 }40 @Test41 public void should_verify_with_after_and_fail() {42 // given43 async.runAfter(10, callMock);44 async.runAfter(40, callMock);45 // then46 Assertions.assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {47 @Override48 public void call() {49 Mockito.verify(mock, Mockito.after(600)).oneArg('1');50 }51 }).isInstanceOf(TooManyActualInvocations.class);52 }53 @Test54 public void should_verify_with_time_x() {55 // given56 async.runAfter(10, callMock);57 async.runAfter(50, callMock);58 async.runAfter(600, callMock);59 // then60 Mockito.verify(mock, Mockito.after(300).times(2)).oneArg('1');61 }62 @Test63 public void should_verify_with_time_x_and_fail() {64 // given65 async.runAfter(10, callMock);66 async.runAfter(40, callMock);67 async.runAfter(80, callMock);68 // then69 Assertions.assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {70 @Override71 public void call() {72 Mockito.verify(mock, Mockito.after(300).times(2)).oneArg('1');73 }74 }).isInstanceOf(TooManyActualInvocations.class);75 }76 @Test77 public void should_verify_with_at_least() {78 // given79 async.runAfter(10, callMock);80 async.runAfter(50, callMock);81 // then82 Mockito.verify(mock, Mockito.after(300).atLeastOnce()).oneArg('1');83 }84 @Test85 public void should_verify_with_at_least_and_fail() {86 // given87 async.runAfter(10, callMock);88 async.runAfter(50, callMock);89 async.runAfter(600, callMock);90 // then91 Assertions.assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {92 @Override93 public void call() {94 Mockito.verify(mock, Mockito.after(300).atLeast(3)).oneArg('1');95 }96 }).isInstanceOf(AssertionError.class).hasMessageContaining("Wanted *at least* 3 times");// TODO specific exception97 }98 @Test99 public void should_verify_with_at_most() {100 // given101 async.runAfter(10, callMock);102 async.runAfter(50, callMock);103 async.runAfter(600, callMock);104 // then105 Mockito.verify(mock, Mockito.after(300).atMost(2)).oneArg('1');106 }107 @Test108 public void should_verify_with_at_most_and_fail() {109 // given110 async.runAfter(10, callMock);111 async.runAfter(50, callMock);112 async.runAfter(600, callMock);113 // then114 Assertions.assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {115 @Override116 public void call() {117 Mockito.verify(mock, Mockito.after(300).atMost(1)).oneArg('1');118 }119 }).isInstanceOf(AssertionError.class).hasMessageContaining("Wanted at most 1 time but was 2");// TODO specific exception120 }121 @Test122 public void should_verify_with_never() {123 // given124 async.runAfter(500, callMock);125 // then126 Mockito.verify(mock, Mockito.after(50).never()).oneArg('1');127 }128 @Test129 public void should_verify_with_never_and_fail() {130 // given131 async.runAfter(10, callMock);132 // then133 Assertions.assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {134 @Override135 public void call() {136 Mockito.verify(mock, Mockito.after(300).never()).oneArg('1');137 }138 }).isInstanceOf(MoreThanAllowedActualInvocations.class).hasMessageContaining("Wanted at most 0 times but was 1");139 }140 @Test141 public void should_verify_with_only() {142 // given143 async.runAfter(10, callMock);144 async.runAfter(600, callMock);145 // then146 Mockito.verify(mock, Mockito.after(300).only()).oneArg('1');147 }148 @Test149 public void should_verify_with_only_and_fail() {150 // given151 async.runAfter(10, callMock);152 async.runAfter(50, callMock);153 // then154 Assertions.assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {155 @Override156 public void call() {157 Mockito.verify(mock, Mockito.after(300).only()).oneArg('1');158 }159 }).isInstanceOf(AssertionError.class).hasMessageContaining("No interactions wanted here");// TODO specific exception160 }161 @Test162 public void should_fail_early_when_at_most_is_used() {163 watch.start();164 // when165 async.runAfter(50, callMock);166 async.runAfter(100, callMock);167 // then168 assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {169 public void call() {170 Mockito.verify(mock, Mockito.after(10000).atMost(1)).oneArg('1');171 }172 }).isInstanceOf(MoreThanAllowedActualInvocations.class);173 // using generous number to avoid timing issues174 watch.assertElapsedTimeIsLessThan(2000, TimeUnit.MILLISECONDS);175 }176 @Test177 public void should_fail_early_when_never_is_used() {178 watch.start();179 // when180 async.runAfter(50, callMock);181 // then182 assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {183 public void call() {184 Mockito.verify(mock, Mockito.after(10000).never()).oneArg('1');185 }186 }).isInstanceOf(MoreThanAllowedActualInvocations.class);187 // using generous number to avoid timing issues188 watch.assertElapsedTimeIsLessThan(2000, TimeUnit.MILLISECONDS);189 }190}...
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!!