Best Mockito code snippet using org.mockitousage.strictness.StrictnessPerMockTest.call
Source:StrictnessPerStubbingTest.java
...35 public void potential_stubbing_problem() {36 //when37 when(mock.simpleMethod("1")).thenReturn("1");38 lenient().when(mock.differentMethod("2")).thenReturn("2");39 //then on lenient stubbing, we can call it with different argument:40 mock.differentMethod("200");41 //but on strict stubbing, we cannot:42 assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {43 @Override44 public void call() throws Throwable {45 ProductionCode.simpleMethod(mock, "100");46 }47 }).isInstanceOf(PotentialStubbingProblem.class);48 }49 @Test50 public void doReturn_syntax() {51 //when52 lenient().doReturn("2").doReturn("3")53 .when(mock).simpleMethod(1);54 //then on lenient stubbing, we can call it with different argument:55 mock.simpleMethod(200);56 //and stubbing works, too:57 assertEquals("2", mock.simpleMethod(1));58 assertEquals("3", mock.simpleMethod(1));59 }60 @Test61 public void doReturn_varargs_syntax() {62 //when63 lenient().doReturn("2", "3")64 .when(mock).simpleMethod(1);65 //then on lenient stubbing, we can call it with different argument with no exception:66 mock.simpleMethod(200);67 //and stubbing works, too:68 assertEquals("2", mock.simpleMethod(1));69 assertEquals("3", mock.simpleMethod(1));70 }71 @Test72 public void doThrow_syntax() {73 //when74 lenient()75 .doThrow(IllegalArgumentException.class)76 .doThrow(IllegalStateException.class)77 .when(mock).simpleMethod(1);78 //then on lenient stubbing, we can call it with different argument with no exception:79 mock.simpleMethod(200);80 //and stubbing works, too:81 assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {82 public void call() throws Throwable {83 mock.simpleMethod(1);84 }85 }).isInstanceOf(IllegalArgumentException.class);86 //testing consecutive call:87 assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {88 public void call() throws Throwable {89 mock.simpleMethod(1);90 }91 }).isInstanceOf(IllegalStateException.class);92 }93 @Test94 public void doThrow_vararg_syntax() {95 //when96 lenient()97 .doThrow(IllegalArgumentException.class, IllegalStateException.class)98 .when(mock).simpleMethod(1);99 //then on lenient stubbing, we can call it with different argument with no exception:100 mock.simpleMethod(200);101 //and stubbing works, too:102 assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {103 public void call() throws Throwable {104 mock.simpleMethod(1);105 }106 }).isInstanceOf(IllegalArgumentException.class);107 //testing consecutive call:108 assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {109 public void call() throws Throwable {110 mock.simpleMethod(1);111 }112 }).isInstanceOf(IllegalStateException.class);113 }114 @Test115 public void doThrow_instance_vararg_syntax() {116 //when117 lenient()118 .doThrow(new IllegalArgumentException(), new IllegalStateException())119 .when(mock).simpleMethod(1);120 //then on lenient stubbing, we can call it with different argument with no exception:121 mock.simpleMethod(200);122 //and stubbing works, too:123 assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {124 public void call() throws Throwable {125 mock.simpleMethod(1);126 }127 }).isInstanceOf(IllegalArgumentException.class);128 //testing consecutive call:129 assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {130 public void call() throws Throwable {131 mock.simpleMethod(1);132 }133 }).isInstanceOf(IllegalStateException.class);134 }135 static class Counter {136 int increment(int x) {137 return x + 1;138 }139 void scream(String message) {140 throw new RuntimeException(message);141 }142 }143 @Test144 public void doCallRealMethod_syntax() {145 //when146 Counter mock = mock(Counter.class);147 lenient().doCallRealMethod().when(mock).increment(1);148 //then no exception and default return value if we call it with different arg:149 assertEquals(0, mock.increment(0));150 //and real method is called when using correct arg:151 assertEquals(2, mock.increment(1));152 }153 @Test154 public void doNothing_syntax() {155 //when156 final Counter spy = spy(Counter.class);157 lenient().doNothing().when(spy).scream("1");158 //then no stubbing exception and real method is called if we call stubbed method with different arg:159 assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {160 @Override161 public void call() throws Throwable {162 spy.scream("2");163 }164 }).hasMessage("2");165 //and we do nothing when stubbing called with correct arg:166 spy.scream("1");167 }168 @Test169 public void doAnswer_syntax() {170 //when171 lenient().doAnswer(AdditionalAnswers.returnsFirstArg()).when(mock).simpleMethod("1");172 //then on lenient stubbing, we can call it with different argument:173 mock.simpleMethod("200");174 //and stubbing works, too:175 assertEquals("1", mock.simpleMethod("1"));176 }177 @Test178 public void unnecessary_stubbing() {179 //when180 when(mock.simpleMethod("1")).thenReturn("1");181 lenient().when(mock.differentMethod("2")).thenReturn("2");182 //then unnecessary stubbing flags method only on the strict stubbing:183 assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {184 @Override185 public void call() throws Throwable {186 mockito.finishMocking();187 }188 }).isInstanceOf(UnnecessaryStubbingException.class)189 .hasMessageContaining("1. -> ")190 //good enough to prove that we're flagging just one unnecessary stubbing:191 //TODO 792: this assertion is duplicated with StrictnessPerMockTest192 .isNot(TestBase.hasMessageContaining("2. ->"));193 }194 @Test195 public void unnecessary_stubbing_with_doReturn() {196 //when197 lenient().doReturn("2").when(mock).differentMethod("2");198 //then no exception is thrown:199 mockito.finishMocking();200 }201 @Test202 public void verify_no_more_invocations() {203 //when204 when(mock.simpleMethod("1")).thenReturn("1");205 lenient().when(mock.differentMethod("2")).thenReturn("2");206 //and:207 mock.simpleMethod("1");208 mock.differentMethod("200"); // <- different arg209 //then 'verifyNoMoreInteractions' flags the lenient stubbing (called with different arg)210 //and reports it with [?] in the exception message211 assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {212 @Override213 public void call() throws Throwable {214 verifyNoMoreInteractions(mock);215 }216 }).isInstanceOf(NoInteractionsWanted.class)217 .hasMessageContaining("1. ->")218 .hasMessageContaining("2. [?]->");219 //TODO 792: assertion duplicated with StrictnessPerMockTest220 // and we should use assertions based on content of the exception rather than the string221 }222 @After223 public void after() {224 mockito.finishMocking();225 }226}...
Source:StrictnessPerMockTest.java
...45 public void potential_stubbing_problem() {46 //when47 given(lenientMock.simpleMethod(100)).willReturn("100");48 given(strictStubsMock.simpleMethod(100)).willReturn("100");49 //then on lenient mock (created by hand), we can call the stubbed method with different arg:50 lenientMock.simpleMethod(200);51 //and on strict stub mock (created by session), we cannot call stubbed method with different arg:52 Assertions.assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {53 public void call() throws Throwable {54 ProductionCode.simpleMethod(strictStubsMock, 200);55 }56 }).isInstanceOf(PotentialStubbingProblem.class);57 }58 @Test59 public void unnecessary_stubbing() {60 //when61 given(lenientMock.simpleMethod(100)).willReturn("100");62 given(strictStubsMock.simpleMethod(100)).willReturn("100");63 //then unnecessary stubbing flags method only on the strict stub mock:64 Assertions.assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {65 @Override66 public void call() throws Throwable {67 mockito.finishMocking();68 }69 }).isInstanceOf(UnnecessaryStubbingException.class)70 .hasMessageContaining("1. -> ")71 //good enough to prove that we're flagging just one unnecessary stubbing:72 //TODO 792: let's make UnnecessaryStubbingException exception contain the Invocation instance73 //so that we can write clean assertion rather than depending on string74 .isNot(TestBase.hasMessageContaining("2. ->"));75 }76 @Test77 public void verify_no_more_invocations() {78 //when79 given(lenientMock.simpleMethod(100)).willReturn("100");80 given(strictStubsMock.simpleMethod(100)).willReturn("100");81 //and:82 strictStubsMock.simpleMethod(100);83 lenientMock.simpleMethod(100);84 //then 'verifyNoMoreInteractions' ignores strict stub (implicitly verified) but flags the lenient mock85 Assertions.assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {86 @Override87 public void call() throws Throwable {88 verifyNoMoreInteractions(strictStubsMock, lenientMock);89 }90 }).isInstanceOf(NoInteractionsWanted.class)91 .hasMessageContaining("But found this interaction on mock 'iMethods'")92 //TODO 792: let's make NoInteractionsWanted exception contain the Invocation instances93 //so that we can write clean assertion rather than depending on string94 .hasMessageContaining("Actually, above is the only interaction with this mock");95 }96 @After97 public void after() {98 mockito.finishMocking();99 }100}...
call
Using AI Code Generation
1StrictnessPerMockTest obj = new StrictnessPerMockTest();2obj.call();3StrictnessPerMockTest obj = new StrictnessPerMockTest();4obj.call();5StrictnessPerMockTest obj = new StrictnessPerMockTest();6obj.call();7StrictnessPerMockTest obj = new StrictnessPerMockTest();8obj.call();9StrictnessPerMockTest obj = new StrictnessPerMockTest();10obj.call();11StrictnessPerMockTest obj = new StrictnessPerMockTest();12obj.call();13StrictnessPerMockTest obj = new StrictnessPerMockTest();14obj.call();15StrictnessPerMockTest obj = new StrictnessPerMockTest();16obj.call();17StrictnessPerMockTest obj = new StrictnessPerMockTest();18obj.call();19StrictnessPerMockTest obj = new StrictnessPerMockTest();20obj.call();21StrictnessPerMockTest obj = new StrictnessPerMockTest();22obj.call();23StrictnessPerMockTest obj = new StrictnessPerMockTest();
call
Using AI Code Generation
1public class 1 {2 public static void main(String[] args) {3 StrictnessPerMockTest strictnessPerMockTest = new StrictnessPerMockTest();4 strictnessPerMockTest.call();5 }6}7public class 2 {8 public static void main(String[] args) {9 StrictnessPerMockTest strictnessPerMockTest = new StrictnessPerMockTest();10 strictnessPerMockTest.call();11 }12}13public class 3 {14 public static void main(String[] args) {15 StrictnessPerMockTest strictnessPerMockTest = new StrictnessPerMockTest();16 strictnessPerMockTest.call();17 }18}191. -> at org.mockitousage.strictness.StrictnessPerMockTest.call(StrictnessPerMockTest.java:16)202. -> at org.mockitousage.strictness.StrictnessPerMockTest.call(StrictnessPerMockTest.java:16)213. -> at org.mockitousage.strictness.StrictnessPerMockTest.call(StrictnessPerMockTest.java:16)22 at org.mockitousage.strictness.StrictnessPerMockTest.call(StrictnessPerMockTest.java:16)
call
Using AI Code Generation
1public class 1 {2 public static void main(String[] args) {3 StrictnessPerMockTest strictnessPerMockTest = new StrictnessPerMockTest();4 strictnessPerMockTest.call();5 }6}7public class 2 {8 public static void main(String[] args) {9 StrictnessPerMockTest strictnessPerMockTest = new StrictnessPerMockTest();10 strictnessPerMockTest.call();11 }12}13public class 3 {14 public static void main(String[] args) {15 StrictnessPerMockTest strictnessPerMockTest = new StrictnessPerMockTest();16 strictnessPerMockTest.call();17 }18}19public class 4 {20 public static void main(String[] args) {21 StrictnessPerMockTest strictnessPerMockTest = new StrictnessPerMockTest();22 strictnessPerMockTest.call();23 }24}25public class 5 {26 public static void main(String[] args) {27 StrictnessPerMockTest strictnessPerMockTest = new StrictnessPerMockTest();28 strictnessPerMockTest.call();29 }30}31public class 6 {32 public static void main(String[] args) {33 StrictnessPerMockTest strictnessPerMockTest = new StrictnessPerMockTest();34 strictnessPerMockTest.call();35 }36}37public class 7 {38 public static void main(String[] args) {39 StrictnessPerMockTest strictnessPerMockTest = new StrictnessPerMockTest();40 strictnessPerMockTest.call();41 }42}
call
Using AI Code Generation
1package org.mockitousage.strictness;2import org.junit.Test;3import org.mockito.Mock;4import org.mockito.Strictness;5import org.mockitousage.IMethods;6import org.mockitoutil.TestBase;7import static org.mockito.Mockito.*;8public class StrictnessPerMockTest extends TestBase {9 @Mock(strictness = Strictness.STRICT_STUBS)10 private IMethods mock;11 public void should_be_strict_stubs() {12 mock.simpleMethod(100);13 }14}15package org.mockitousage.strictness;16import org.junit.Test;17import org.mockito.Mock;18import org.mockito.Strictness;19import org.mockitousage.IMethods;20import org.mockitoutil.TestBase;21import static org.mockito.Mockito.*;22public class StrictnessPerMockTest extends TestBase {23 @Mock(strictness = Strictness.STRICT_STUBS)24 private IMethods mock;25 public void should_be_strict_stubs() {26 mock.simpleMethod(100);27 }28}29package org.mockitousage.strictness;30import org.junit.Test;31import org.mockito.Mock;32import org.mockito.Strictness;33import org.mockitousage.IMethods;34import org.mockitoutil.TestBase;35import static org.mockito.Mockito.*;36public class StrictnessPerMockTest extends TestBase {37 @Mock(strictness = Strictness.STRICT_STUBS)38 private IMethods mock;39 public void should_be_strict_stubs() {40 mock.simpleMethod(100);41 }42}43package org.mockitousage.strictness;44import org.junit.Test;45import org.mockito.Mock;46import org.mockito.Strictness;47import org.mockitousage.IMethods;48import org.mockitoutil.TestBase;49import static org.mockito.Mockito.*;50public class StrictnessPerMockTest extends TestBase {51 @Mock(strictness = Strictness.STRICT_STUBS)52 private IMethods mock;53 public void should_be_strict_stubs() {54 mock.simpleMethod(100);55 }56}
call
Using AI Code Generation
1package org.mockitousage.strictness;2import org.junit.Test;3import org.mockito.Mock;4import org.mockito.Mockito;5import org.mockito.exceptions.misusing.MissingMethodInvocationException;6import org.mockito.exceptions.misusing.UnfinishedVerificationException;7import org.mockitousage.IMethods;8import org.mockitoutil.TestBase;9public class StrictnessPerMockTest extends TestBase {10 @Mock IMethods mockOne;11 @Mock IMethods mockTwo;12 public void shouldAllowToUseDifferentStrictnessPerMock() {13 Mockito.when(mockOne.simpleMethod()).thenReturn("foo");14 Mockito.when(mockTwo.simpleMethod()).thenReturn("foo");15 Mockito.verify(mockOne).simpleMethod();16 Mockito.verify(mockTwo).simpleMethod();17 }18 @Test(expected = MissingMethodInvocationException.class)19 public void shouldFailWhenMethodIsNotCalledOnStrictMock() {20 Mockito.when(mockOne.simpleMethod()).thenReturn("foo");21 Mockito.verify(mockOne).simpleMethod();22 }23 @Test(expected = UnfinishedVerificationException.class)24 public void shouldFailWhenMethodIsNotCalledOnStrictMockWithExtraMethodCall() {25 Mockito.when(mockOne.simpleMethod()).thenReturn("foo");26 Mockito.verify(mockOne).simpleMethod();27 mockOne.simpleMethod();28 }29}30package org.mockitousage.strictness;31import org.junit.Test;32import org.mockito.Mock;33import org.mockito.Mockito;34import org.mockito.exceptions.misusing.MissingMethodInvocationException;35import org.mockito.exceptions.misusing.UnfinishedVerificationException;36import org.mockitousage.IMethods;37import org.mockitoutil.TestBase;38public class StrictnessPerMockTest extends TestBase {39 @Mock IMethods mockOne;40 @Mock IMethods mockTwo;41 public void shouldAllowToUseDifferentStrictnessPerMock() {42 Mockito.when(mockOne.simpleMethod()).thenReturn("foo");43 Mockito.when(mockTwo.simpleMethod()).thenReturn("foo");44 Mockito.verify(mockOne).simpleMethod();45 Mockito.verify(mockTwo).simpleMethod();46 }47 @Test(expected = MissingMethodInvocationException.class)48 public void shouldFailWhenMethodIsNotCalledOnStrictMock() {49 Mockito.when(mockOne.simpleMethod()).thenReturn("foo");50 Mockito.verify(mockOne).simpleMethod();51 }52 @Test(expected =
call
Using AI Code Generation
1package org.mockitousage.strictness;2import org.junit.Test;3import org.mockito.Mockito;4import org.mockito.exceptions.misusing.MissingMethodInvocationException;5import org.mockito.exceptions.misusing.UnfinishedVerificationException;6import org.mockitousage.IMethods;7import org.mockitoutil.TestBase;8import static org.assertj.core.api.Assertions.assertThat;9import static org.junit.Assert.fail;10import static org.mockito.Mockito.*;11public class StrictnessPerMockTest extends TestBase {12 public void testStrictnessPerMock() {13 IMethods mockOne = mock(IMethods.class, Mockito.withSettings().strictness(Strictness.STRICT_STUBS));14 IMethods mockTwo = mock(IMethods.class, Mockito.withSettings().strictness(Strictness.STRICT_STUBS));15 mockOne.simpleMethod(1);16 mockTwo.simpleMethod(1);17 verify(mockOne).simpleMethod(1);18 verify(mockTwo).simpleMethod(1);19 }20 public void testStrictnessPerMock2() {21 IMethods mockOne = mock(IMethods.class, Mockito.withSettings().strictness(Strictness.STRICT_STUBS));22 IMethods mockTwo = mock(IMethods.class, Mockito.withSettings().strictness(Strictness.STRICT_STUBS));23 mockOne.simpleMethod(1);24 mockTwo.simpleMethod(1);25 verify(mockOne).simpleMethod(1);26 verify(mockTwo).simpleMethod(1);27 }28 public void testStrictnessPerMock3() {29 IMethods mockOne = mock(IMethods.class, Mockito.withSettings().strictness(Strictness.STRICT_STUBS));30 IMethods mockTwo = mock(IMethods.class, Mockito.withSettings().strictness(Strictness.STRICT_STUBS));31 mockOne.simpleMethod(1);32 mockTwo.simpleMethod(1);33 verify(mockOne).simpleMethod(1);34 verify(mockTwo).simpleMethod(1);35 }36 public void testStrictnessPerMock4() {37 IMethods mockOne = mock(IMethods.class, Mockito.withSettings().strictness(Strictness.STRICT_STUBS));38 IMethods mockTwo = mock(IMethods.class, Mockito.withSettings().strictness(Strictness.STRICT_STUBS));39 mockOne.simpleMethod(1);40 mockTwo.simpleMethod(1);
call
Using AI Code Generation
1import org.junit.Test;2import org.mockito.Mockito;3import org.mockito.exceptions.base.MockitoException;4import org.mockito.exceptions.verification.NoInteractionsWanted;5import org.mockito.exceptions.verification.WantedButNotInvoked;6import org.mockitousage.IMethods;7import org.mockitousage.IMethodsImpl;8import org.mockitousage.MethodsImpl;9import org.mockitousage.strictness.StrictnessPerMockTest;10import static org.mockito.Mockito.*;11public class StrictnessPerMockTestTest {12 public void should_allow_strict_stubbing() {13 IMethods mock = mock(IMethods.class, Mockito.withSettings().strictness(Strictness.STRICT_STUBS));14 when(mock.simpleMethod(1)).thenReturn("one");15 verifyNoMoreInteractions(mock);16 }17 @Test(expected = WantedButNotInvoked.class)18 public void should_allow_strict_stubbing2() {19 IMethods mock = mock(IMethods.class, Mockito.withSettings().strictness(Strictness.STRICT_STUBS));20 when(mock.simpleMethod(1)).thenReturn("one");21 verify(mock).simpleMethod(1);22 verifyNoMoreInteractions(mock);23 }24 @Test(expected = WantedButNotInvoked.class)25 public void should_allow_strict_stubbing3() {26 IMethods mock = mock(IMethods.class, Mockito.withSettings().strictness(Strictness.STRICT_STUBS));27 when(mock.simpleMethod(1)).thenReturn("one");28 verify(mock).simpleMethod(1);29 }30 @Test(expected = NoInteractionsWanted.class)31 public void should_allow_strict_stubbing4() {32 IMethods mock = mock(IMethods.class, Mockito.withSettings().strictness(Strictness.STRICT_STUBS));33 when(mock.simpleMethod(1)).thenReturn("one");34 mock.simpleMethod(1);35 verifyNoMoreInteractions(mock);36 }37 public void should_allow_strict_stubbing5() {38 IMethods mock = mock(IMethods.class, Mockito.withSettings().strictness(Strictness.STRICT_STUBS));39 when(mock.simpleMethod(1)).thenReturn("one");40 mock.simpleMethod(1);41 verify(mock).simpleMethod(1);42 }43 public void should_allow_strict_stubbing6() {
call
Using AI Code Generation
1public class 1 {2 public void test() {3 StrictnessPerMockTest mock = mock(StrictnessPerMockTest.class);4 mock.call();5 }6}7public class 2 {8 public void test() {9 StrictnessPerMockTest mock = mock(StrictnessPerMockTest.class);10 mock.call();11 }12}13public class 3 {14 public void test() {15 StrictnessPerMockTest mock = mock(StrictnessPerMockTest.class);16 mock.call();17 }18}19public class 4 {20 public void test() {21 StrictnessPerMockTest mock = mock(StrictnessPerMockTest.class);22 mock.call();23 }24}25public class 5 {26 public void test() {27 StrictnessPerMockTest mock = mock(StrictnessPerMockTest.class);28 mock.call();29 }30}31public class 6 {32 public void test() {33 StrictnessPerMockTest mock = mock(StrictnessPerMockTest.class);34 mock.call();35 }36}37public class 7 {38 public void test() {39 StrictnessPerMockTest mock = mock(StrictnessPerMockTest.class);40 mock.call();41 }42}43public class 8 {44 public void test() {45 StrictnessPerMockTest mock = mock(StrictnessPerMockTest.class);46 mock.call();
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!!