Home | History | Annotate | Download | only in strictness
      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.mock;
     21 import static org.mockito.Mockito.when;
     22 import static org.mockito.Mockito.withSettings;
     23 
     24 public class StrictnessWhenRuleStrictnessIsUpdatedTest {
     25 
     26     @Mock IMethods mock;
     27     @Rule public MockitoRule rule = MockitoJUnit.rule().strictness(Strictness.LENIENT);
     28 
     29     @Test
     30     public void strictness_per_mock() {
     31         //when
     32         rule.strictness(Strictness.STRICT_STUBS);
     33 
     34         //then previous mock is strict:
     35         when(mock.simpleMethod(1)).thenReturn("1");
     36         assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {
     37             @Override
     38             public void call() throws Throwable {
     39                 mock.simpleMethod(2);
     40             }
     41         }).isInstanceOf(PotentialStubbingProblem.class);
     42 
     43         //but the new mock is lenient, even though the rule is not:
     44         final IMethods lenientMock = mock(IMethods.class, withSettings().lenient());
     45         when(lenientMock.simpleMethod(1)).thenReturn("1");
     46         lenientMock.simpleMethod(100);
     47     }
     48 
     49     @Test
     50     public void strictness_per_stubbing() {
     51         //when
     52         rule.strictness(Strictness.STRICT_STUBS);
     53 
     54         //then previous mock is strict:
     55         when(mock.simpleMethod(1)).thenReturn("1");
     56         assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {
     57             @Override
     58             public void call() throws Throwable {
     59                 mock.simpleMethod(2);
     60             }
     61         }).isInstanceOf(PotentialStubbingProblem.class);
     62 
     63         //but the new mock is lenient, even though the rule is not:
     64         lenient().when(mock.simpleMethod(1)).thenReturn("1");
     65         mock.simpleMethod(100);
     66     }
     67 }
     68