diff --git a/testing-modules/mockito-2/src/test/java/com/baeldung/mockito/misusing/ExpectedTestFailureRule.java b/testing-modules/mockito-2/src/test/java/com/baeldung/mockito/misusing/ExpectedTestFailureRule.java new file mode 100644 index 0000000000..53ad60a2fa --- /dev/null +++ b/testing-modules/mockito-2/src/test/java/com/baeldung/mockito/misusing/ExpectedTestFailureRule.java @@ -0,0 +1,51 @@ +package com.baeldung.mockito.misusing; + +import org.hamcrest.Matchers; +import org.junit.Assert; +import org.junit.rules.MethodRule; +import org.junit.runners.model.FrameworkMethod; +import org.junit.runners.model.Statement; + +public class ExpectedTestFailureRule implements MethodRule { + + private final MethodRule testedRule; + private FailureAssert failureAssert = null; + + public ExpectedTestFailureRule(MethodRule testedRule) { + this.testedRule = testedRule; + } + + @Override + public Statement apply(final Statement base, final FrameworkMethod method, final Object target) { + return new Statement() { + public void evaluate() throws Throwable { + try { + testedRule.apply(base, method, target) + .evaluate(); + } catch (Throwable t) { + if (failureAssert == null) { + throw t; + } + failureAssert.doAssert(t); + return; + } + } + }; + } + + @SuppressWarnings("unchecked") + public void expectedFailure(final Class expected) { + FailureAssert assertion = t -> Assert.assertThat(t, Matchers.isA((Class) expected)); + this.expectedFailure(assertion); + } + + private void expectedFailure(FailureAssert failureAssert) { + this.failureAssert = failureAssert; + } + + @FunctionalInterface + private interface FailureAssert { + abstract void doAssert(Throwable t); + } + +} diff --git a/testing-modules/mockito-2/src/test/java/com/baeldung/mockito/misusing/MockitoUnecessaryStubUnitTest.java b/testing-modules/mockito-2/src/test/java/com/baeldung/mockito/misusing/MockitoUnecessaryStubUnitTest.java new file mode 100644 index 0000000000..00edb699de --- /dev/null +++ b/testing-modules/mockito-2/src/test/java/com/baeldung/mockito/misusing/MockitoUnecessaryStubUnitTest.java @@ -0,0 +1,52 @@ +package com.baeldung.mockito.misusing; + +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.exceptions.misusing.UnnecessaryStubbingException; +import org.mockito.junit.MockitoJUnit; +import org.mockito.quality.Strictness; + +public class MockitoUnecessaryStubUnitTest { + + @Rule + public ExpectedTestFailureRule rule = new ExpectedTestFailureRule(MockitoJUnit.rule() + .strictness(Strictness.STRICT_STUBS)); + + @Mock + private ArrayList mockList; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + } + + @Test + public void givenUnusedStub_whenInvokingGetThenThrowUnnecessaryStubbingException() { + rule.expectedFailure(UnnecessaryStubbingException.class); + + when(mockList.add("one")).thenReturn(true); + when(mockList.get(anyInt())).thenReturn("hello"); + + assertEquals("List should contain hello", "hello", mockList.get(1)); + } + + @Test + public void givenLenientdStub_whenInvokingGetThenDontThrowUnnecessaryStubbingException() { + lenient().when(mockList.add("one")) + .thenReturn(true); + when(mockList.get(anyInt())).thenReturn("hello"); + + assertEquals("List should contain hello", "hello", mockList.get(1)); + } + +}