1 /* 2 * Copyright (c) 2007 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 6 package org.mockitousage.strictness; 7 8 import org.assertj.core.api.ThrowableAssert; 9 import org.junit.Rule; 10 import org.junit.Test; 11 import org.mockito.Mock; 12 import org.mockito.exceptions.misusing.PotentialStubbingProblem; 13 import org.mockito.junit.MockitoJUnit; 14 import org.mockito.junit.MockitoRule; 15 import org.mockito.quality.Strictness; 16 import org.mockitousage.IMethods; 17 18 import static org.assertj.core.api.Assertions.assertThatThrownBy; 19 import static org.mockito.Mockito.lenient; 20 import static org.mockito.Mockito.when; 21 22 public class StrictnessWithRulesTest { 23 24 @Mock IMethods mock; 25 @Rule public MockitoRule rule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS); 26 27 @Test potential_stubbing_problem()28 public void potential_stubbing_problem() { 29 //when 30 when(mock.simpleMethod("1")).thenReturn("1"); 31 lenient().when(mock.differentMethod("2")).thenReturn("2"); 32 33 //then on lenient stubbing, we can call it with different argument: 34 mock.differentMethod("200"); 35 36 //but on strict stubbing, we cannot: 37 assertThatThrownBy(new ThrowableAssert.ThrowingCallable() { 38 @Override 39 public void call() throws Throwable { 40 mock.simpleMethod("100"); 41 } 42 }).isInstanceOf(PotentialStubbingProblem.class); 43 44 //let's use the strict stubbing so that it is not reported as failure by the rule: 45 mock.simpleMethod("1"); 46 } 47 48 @Test unnecessary_stubbing()49 public void unnecessary_stubbing() { 50 //this unnecessary stubbing is not flagged by the rule: 51 lenient().when(mock.differentMethod("2")).thenReturn("2"); 52 } 53 } 54