How to use StrictnessPerStubbingTest class of org.mockitousage.strictness package

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

Source:StrictnessPerStubbingTest.java Github

copy

Full Screen

...14import org.mockito.exceptions.misusing.UnnecessaryStubbingException;15import org.mockito.exceptions.verification.NoInteractionsWanted;16import org.mockitousage.IMethods;17import org.mockitoutil.TestBase;18public class StrictnessPerStubbingTest {19 MockitoSession mockito;20 @Mock21 IMethods mock;22 @Test23 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 // when...

Full Screen

Full Screen

StrictnessPerStubbingTest

Using AI Code Generation

copy

Full Screen

1package org.mockitousage.strictness;2import org.junit.Before;3import org.junit.Test;4import org.mockito.Mock;5import org.mockito.Mockito;6import org.mockito.exceptions.misusing.UnnecessaryStubbingException;7import org.mockitousage.IMethods;8import org.mockitoutil.TestBase;9import static org.junit.Assert.assertEquals;10import static org.mockito.Mockito.*;11public class StrictnessPerStubbingTest extends TestBase {12 @Mock IMethods mock;13 public void setup() {14 Mockito.reset(mock);15 }16 public void should_allow_stubbing_only_selected_methods() {17 when(mock.simpleMethod()).thenReturn("foo");18 when(mock.otherMethod()).thenThrow(new RuntimeException());19 assertEquals("foo", mock.simpleMethod());20 try {21 mock.otherMethod();22 fail();23 } catch (RuntimeException e) {}24 }25 public void should_allow_stubbing_only_selected_methods_in_verify() {26 mock.simpleMethod();27 mock.otherMethod();28 verify(mock).simpleMethod();29 try {30 verify(mock).otherMethod();31 fail();32 } catch (UnnecessaryStubbingException e) {}33 }34}35package org.mockitousage.strictness;36import org.junit.Before;37import org.junit.Test;38import org.mockito.Mock;39import org.mockito.Mockito;40import org.mockito.exceptions.misusing.UnnecessaryStubbingException;41import org.mockitousage.IMethods;42import org.mockitoutil.TestBase;43import static org.junit.Assert.assertEquals;44import static org.mockito.Mockito.*;45public class StrictnessPerStubbingTest extends TestBase {46 @Mock IMethods mock;47 public void setup() {48 Mockito.reset(mock);49 }50 public void should_allow_stubbing_only_selected_methods() {51 when(mock.simpleMethod()).thenReturn("foo");52 when(mock.otherMethod()).thenThrow(new RuntimeException());53 assertEquals("foo", mock.simpleMethod());54 try {55 mock.otherMethod();56 fail();57 } catch (RuntimeException e) {}58 }59 public void should_allow_stubbing_only_selected_methods_in_verify() {60 mock.simpleMethod();61 mock.otherMethod();62 verify(mock).simpleMethod();63 try {64 verify(mock).otherMethod();65 fail();66 } catch (UnnecessaryStubbingException e) {}67 }68}

Full Screen

Full Screen

StrictnessPerStubbingTest

Using AI Code Generation

copy

Full Screen

1package org.mockitousage.strictness;2import org.junit.Test;3import org.mockito.Mock;4import org.mockito.exceptions.base.MockitoException;5import org.mockito.exceptions.verification.NoInteractionsWanted;6import org.mockito.exceptions.verification.TooLittleActualInvocations;7import org.mockito.exceptions.verification.TooManyActualInvocations;8import org.mockito.exceptions.verification.WantedButNotInvoked;9import org.mockito.internal.util.MockUtil;10import org.mockito.junit.MockitoJUnitRunner;11import org.mockito.quality.Strictness;12import org.mockito.quality.StrictnessPerStubbingTest;13import org.mockitousage.IMethods;14import org.mockitoutil.TestBase;15import static org.junit.Assert.assertEquals;16import static org.junit.Assert.assertTrue;17import static org.junit.Assert.fail;18import static org.mockito.Mockito.*;19@StrictnessPerStubbingTest(Strictness.LENIENT)20public class StrictnessPerStubbingTest extends TestBase {21 private IMethods mock;22 public void should_not_throw_exception_when_no_interaction() {23 when(mock.simpleMethod()).thenReturn("foo");24 }25 public void should_not_throw_exception_when_too_few_invocations() {26 when(mock.simpleMethod()).thenReturn("foo");27 mock.simpleMethod();28 }29 public void should_not_throw_exception_when_too_many_invocations() {30 when(mock.simpleMethod()).thenReturn("foo");31 mock.simpleMethod();32 mock.simpleMethod();33 }34 public void should_not_throw_exception_when_no_interaction_with_wanted_but_not_invoked() {35 when(mock.simpleMethod()).thenReturn("foo");36 verify(mock).simpleMethod();37 }38 public void should_not_throw_exception_when_too_few_invocations_with_wanted_but_not_invoked() {39 when(mock.simpleMethod()).thenReturn("foo");40 mock.simpleMethod();41 verify(mock).simpleMethod();42 }43 public void should_not_throw_exception_when_too_many_invocations_with_wanted_but_not_invoked() {44 when(mock.simple

Full Screen

Full Screen

StrictnessPerStubbingTest

Using AI Code Generation

copy

Full Screen

1package org.mockitousage.strictness;2import org.junit.Test;3import org.mockito.Mock;4import org.mockito.exceptions.misusing.Strictness;5import org.mockito.exceptions.misusing.UnnecessaryStubbingException;6import org.mockito.junit.MockitoJUnit;7import org.mockito.junit.MockitoRule;8import org.mockitousage.IMethods;9import org.mockitoutil.TestBase;10import static org.mockito.Mockito.*;11public class StrictnessPerStubbingTest extends TestBase {12 IMethods mock;13 public void should_not_throw_when_stubbing_is_not_used() {14 when(mock.simpleMethod()).thenReturn("foo");15 }16 public void should_not_throw_when_stubbing_is_used() {17 when(mock.simpleMethod()).thenReturn("foo");18 mock.simpleMethod();19 }20 @Test(expected = UnnecessaryStubbingException.class)21 public void should_throw_when_stubbing_is_not_used_and_strictness_is_STRICT_STUBS() {22 when(mock.simpleMethod()).thenReturn("foo");23 MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS).apply(this, null).evaluate();24 }25}26package org.mockitousage.strictness;27import org.junit.Test;28import org.mockito.Mock;29import org.mockito.exceptions.misusing.Strictness;30import org.mockito.exceptions.misusing.UnnecessaryStubbingException;31import org.mockito.junit.MockitoJUnit;32import org.mockito.junit.MockitoRule;33import org.mockitousage.IMethods;34import org.mockitoutil.TestBase;35import static org.mockito.Mockito.*;36public class StrictnessPerStubbingTest extends TestBase {37 IMethods mock;38 public void should_not_throw_when_stubbing_is_not_used() {39 when(mock.simpleMethod()).thenReturn("foo");40 }41 public void should_not_throw_when_stubbing_is_used() {42 when(mock.simpleMethod()).thenReturn("foo");43 mock.simpleMethod();44 }45 @Test(expected = UnnecessaryStubbingException.class)46 public void should_throw_when_stubbing_is_not_used_and_strictness_is_STRICT_STUBS() {47 when(mock.simpleMethod()).thenReturn("foo");48 MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS).apply(this, null).evaluate();49 }50}

Full Screen

Full Screen

StrictnessPerStubbingTest

Using AI Code Generation

copy

Full Screen

1testStrictnessPerStubbing()2testStrictnessPerStubbingWithAnnotations()3testStrictnessPerStubbingWithAnnotationsOnClass()4testStrictnessPerStubbingWithAnnotationsOnClassAndMethod()5testStrictnessPerStubbingWithAnnotationsOnMethod()6testStrictnessPerStubbingWithAnnotationsOnMethodAndClass()7testStrictnessPerStubbingWithAnnotationsOnMethodAndClass2()8testStrictnessPerStubbingWithAnnotationsOnMethodAndClass3()9testStrictnessPerStubbingWithAnnotationsOnMethodAndClass4()10testStrictnessPerStubbingWithAnnotationsOnMethodAndClass5()11testStrictnessPerStubbingWithAnnotationsOnMethodAndClass6()12testStrictnessPerStubbingWithAnnotationsOnMethodAndClass7()13testStrictnessPerStubbingWithAnnotationsOnMethodAndClass8()14testStrictnessPerStubbingWithAnnotationsOnMethodAndClass9()15testStrictnessPerStubbingWithAnnotationsOnMethodAndClass10()16testStrictnessPerStubbingWithAnnotationsOnMethodAndClass11()17testStrictnessPerStubbingWithAnnotationsOnMethodAndClass12()18testStrictnessPerStubbingWithAnnotationsOnMethodAndClass13()19testStrictnessPerStubbingWithAnnotationsOnMethodAndClass14()20testStrictnessPerStubbingWithAnnotationsOnMethodAndClass15()21testStrictnessPerStubbingWithAnnotationsOnMethodAndClass16()22testStrictnessPerStubbingWithAnnotationsOnMethodAndClass17()23testStrictnessPerStubbingWithAnnotationsOnMethodAndClass18()24testStrictnessPerStubbingWithAnnotationsOnMethodAndClass19()25testStrictnessPerStubbingWithAnnotationsOnMethodAndClass20()26testStrictnessPerStubbingWithAnnotationsOnMethodAndClass21()27testStrictnessPerStubbingWithAnnotationsOnMethodAndClass22()28testStrictnessPerStubbingWithAnnotationsOnMethodAndClass23()29testStrictnessPerStubbingWithAnnotationsOnMethodAndClass24()30testStrictnessPerStubbingWithAnnotationsOnMethodAndClass25()31testStrictnessPerStubbingWithAnnotationsOnMethodAndClass26()32testStrictnessPerStubbingWithAnnotationsOnMethodAndClass27()33testStrictnessPerStubbingWithAnnotationsOnMethodAndClass28()34testStrictnessPerStubbingWithAnnotationsOnMethodAndClass29()

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.

Run Mockito automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful