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.Test; 10 import org.junit.runner.RunWith; 11 import org.mockito.Mock; 12 import org.mockito.exceptions.misusing.PotentialStubbingProblem; 13 import org.mockito.junit.MockitoJUnitRunner; 14 import org.mockitousage.IMethods; 15 16 import static org.assertj.core.api.Assertions.assertThatThrownBy; 17 import static org.mockito.Mockito.lenient; 18 import static org.mockito.Mockito.when; 19 20 @RunWith(MockitoJUnitRunner.StrictStubs.class) 21 public class StrictnessPerStubbingWithRunnerTest { 22 23 @Mock IMethods mock; 24 25 @Test potential_stubbing_problem()26 public void potential_stubbing_problem() { 27 //when 28 when(mock.simpleMethod("1")).thenReturn("1"); 29 lenient().when(mock.differentMethod("2")).thenReturn("2"); 30 31 //then on lenient stubbing, we can call it with different argument: 32 mock.differentMethod("200"); 33 34 //but on strict stubbing, we cannot: 35 assertThatThrownBy(new ThrowableAssert.ThrowingCallable() { 36 @Override 37 public void call() throws Throwable { 38 mock.simpleMethod("100"); 39 } 40 }).isInstanceOf(PotentialStubbingProblem.class); 41 42 //let's use the strict stubbing so that it is not reported as failure by the runner: 43 mock.simpleMethod("1"); 44 } 45 46 @Test unnecessary_stubbing()47 public void unnecessary_stubbing() { 48 //this unnecessary stubbing is not flagged by the runner: 49 lenient().when(mock.differentMethod("2")).thenReturn("2"); 50 } 51 } 52