How to use call method of org.mockitousage.strictness.StrictnessPerStubbingTest class

Best Mockito code snippet using org.mockitousage.strictness.StrictnessPerStubbingTest.call

Source:StrictnessPerStubbingTest.java Github

copy

Full Screen

...23 public void potential_stubbing_problem() {24 // when25 Mockito.when(mock.simpleMethod("1")).thenReturn("1");26 Mockito.lenient().when(mock.differentMethod("2")).thenReturn("2");27 // then on lenient stubbing, we can call it with different argument:28 mock.differentMethod("200");29 // but on strict stubbing, we cannot:30 assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {31 @Override32 public void call() throws Throwable {33 ProductionCode.simpleMethod(mock, "100");34 }35 }).isInstanceOf(PotentialStubbingProblem.class);36 }37 @Test38 public void doReturn_syntax() {39 // when40 Mockito.lenient().doReturn("2").doReturn("3").when(mock).simpleMethod(1);41 // then on lenient stubbing, we can call it with different argument:42 mock.simpleMethod(200);43 // and stubbing works, too:44 Assert.assertEquals("2", mock.simpleMethod(1));45 Assert.assertEquals("3", mock.simpleMethod(1));46 }47 @Test48 public void doReturn_varargs_syntax() {49 // when50 Mockito.lenient().doReturn("2", "3").when(mock).simpleMethod(1);51 // then on lenient stubbing, we can call it with different argument with no exception:52 mock.simpleMethod(200);53 // and stubbing works, too:54 Assert.assertEquals("2", mock.simpleMethod(1));55 Assert.assertEquals("3", mock.simpleMethod(1));56 }57 @Test58 public void doThrow_syntax() {59 // when60 Mockito.lenient().doThrow(IllegalArgumentException.class).doThrow(IllegalStateException.class).when(mock).simpleMethod(1);61 // then on lenient stubbing, we can call it with different argument with no exception:62 mock.simpleMethod(200);63 // and stubbing works, too:64 assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {65 public void call() throws Throwable {66 mock.simpleMethod(1);67 }68 }).isInstanceOf(IllegalArgumentException.class);69 // testing consecutive call:70 assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {71 public void call() throws Throwable {72 mock.simpleMethod(1);73 }74 }).isInstanceOf(IllegalStateException.class);75 }76 @Test77 public void doThrow_vararg_syntax() {78 // when79 Mockito.lenient().doThrow(IllegalArgumentException.class, IllegalStateException.class).when(mock).simpleMethod(1);80 // then on lenient stubbing, we can call it with different argument with no exception:81 mock.simpleMethod(200);82 // and stubbing works, too:83 assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {84 public void call() throws Throwable {85 mock.simpleMethod(1);86 }87 }).isInstanceOf(IllegalArgumentException.class);88 // testing consecutive call:89 assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {90 public void call() throws Throwable {91 mock.simpleMethod(1);92 }93 }).isInstanceOf(IllegalStateException.class);94 }95 @Test96 public void doThrow_instance_vararg_syntax() {97 // when98 Mockito.lenient().doThrow(new IllegalArgumentException(), new IllegalStateException()).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 static class Counter {115 int increment(int x) {116 return x + 1;117 }118 void scream(String message) {119 throw new RuntimeException(message);120 }121 }122 @Test123 public void doCallRealMethod_syntax() {124 // when125 StrictnessPerStubbingTest.Counter mock = Mockito.mock(StrictnessPerStubbingTest.Counter.class);126 Mockito.lenient().doCallRealMethod().when(mock).increment(1);127 // then no exception and default return value if we call it with different arg:128 Assert.assertEquals(0, mock.increment(0));129 // and real method is called when using correct arg:130 Assert.assertEquals(2, mock.increment(1));131 }132 @Test133 public void doNothing_syntax() {134 // when135 final StrictnessPerStubbingTest.Counter spy = Mockito.spy(StrictnessPerStubbingTest.Counter.class);136 Mockito.lenient().doNothing().when(spy).scream("1");137 // then no stubbing exception and real method is called if we call stubbed method with different arg:138 assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {139 @Override140 public void call() throws Throwable {141 spy.scream("2");142 }143 }).hasMessage("2");144 // and we do nothing when stubbing called with correct arg:145 spy.scream("1");146 }147 @Test148 public void doAnswer_syntax() {149 // when150 Mockito.lenient().doAnswer(AdditionalAnswers.returnsFirstArg()).when(mock).simpleMethod("1");151 // then on lenient stubbing, we can call it with different argument:152 mock.simpleMethod("200");153 // and stubbing works, too:154 Assert.assertEquals("1", mock.simpleMethod("1"));155 }156 @Test157 public void unnecessary_stubbing() {158 // when159 Mockito.when(mock.simpleMethod("1")).thenReturn("1");160 Mockito.lenient().when(mock.differentMethod("2")).thenReturn("2");161 // then unnecessary stubbing flags method only on the strict stubbing:162 // good enough to prove that we're flagging just one unnecessary stubbing:163 // TODO 792: this assertion is duplicated with StrictnessPerMockTest164 assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {165 @Override166 public void call() throws Throwable {167 mockito.finishMocking();168 }169 }).isInstanceOf(UnnecessaryStubbingException.class).hasMessageContaining("1. -> ").isNot(TestBase.hasMessageContaining("2. ->"));170 }171 @Test172 public void unnecessary_stubbing_with_doReturn() {173 // when174 Mockito.lenient().doReturn("2").when(mock).differentMethod("2");175 // then no exception is thrown:176 mockito.finishMocking();177 }178 @Test179 public void verify_no_more_invocations() {180 // when181 Mockito.when(mock.simpleMethod("1")).thenReturn("1");182 Mockito.lenient().when(mock.differentMethod("2")).thenReturn("2");183 // and:184 mock.simpleMethod("1");185 mock.differentMethod("200");// <- different arg186 // then 'verifyNoMoreInteractions' flags the lenient stubbing (called with different arg)187 // and reports it with [?] in the exception message188 assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {189 @Override190 public void call() throws Throwable {191 Mockito.verifyNoMoreInteractions(mock);192 }193 }).isInstanceOf(NoInteractionsWanted.class).hasMessageContaining("1. ->").hasMessageContaining("2. [?]->");194 // TODO 792: assertion duplicated with StrictnessPerMockTest195 // and we should use assertions based on content of the exception rather than the string196 }197}...

Full Screen

Full Screen

call

Using AI Code Generation

copy

Full Screen

1import org.junit.jupiter.api.Test;2import org.junit.jupiter.api.extension.ExtendWith;3import org.mockito.junit.jupiter.MockitoExtension;4import static org.mockito.Mockito.mock;5import static org.mockito.Mockito.when;6@ExtendWith(MockitoExtension.class)7class StrictnessPerStubbingTest {8 void test() {9 StrictnessPerStubbingTest mock = mock(StrictnessPerStubbingTest.class);10 when(mock.call()).thenReturn(1);11 when(mock.call()).thenReturn(2);12 }13}14 when(mock.getArticles()).thenReturn(articles);15at org.mockito.exceptions.misusing.MissingMethodInvocationException.create(MissingMethodInvocationException.java:24)16at org.mockito.exceptions.misusing.MissingMethodInvocationException.create(MissingMethodInvocationException.java:14)17at org.mockito.internal.verification.api.VerificationDataImpl.inOrderContext(VerificationDataImpl.java:68)18at org.mockito.internal.verification.api.VerificationDataImpl.inOrderContext(VerificationDataImpl.java:9)19at org.mockito.internal.verification.VerificationDataInOrderImpl.inOrderContext(VerificationDataInOrderImpl.java:12)20at org.mockito.internal.verification.VerificationDataInOrderImpl.inOrderContext(VerificationDataInOrderImpl.java:6)21at org.mockito.internal.verification.api.VerificationDataImpl.inOrderContext(VerificationDataImpl.java:64)22at org.mockito.internal.verification.api.VerificationDataImpl.inOrderContext(VerificationDataImpl.java:9)23at org.mockito.internal.verification.VerificationDataInOrderImpl.inOrderContext(VerificationDataInOrderImpl.java:12)24at org.mockito.internal.verification.VerificationDataInOrderImpl.inOrderContext(VerificationDataInOrderImpl.java:6)25at org.mockito.internal.verification.VerificationDataInOrderImpl.buildVerificationMode(VerificationDataInOrderImpl.java:19)26at org.mockito.internal.verification.VerificationDataInOrderImpl.buildVerificationMode(

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful