• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 package org.mockitousage.junitrule;
6 
7 import org.junit.Rule;
8 import org.junit.Test;
9 import org.junit.runner.JUnitCore;
10 import org.junit.runner.Result;
11 import org.mockito.Mock;
12 import org.mockito.junit.MockitoJUnit;
13 import org.mockito.junit.MockitoRule;
14 import org.mockito.quality.Strictness;
15 import org.mockitousage.IMethods;
16 import org.mockitoutil.JUnitResultAssert;
17 
18 import static org.mockito.Mockito.when;
19 
20 public class MutableStrictJUnitRuleTest {
21 
22     JUnitCore runner = new JUnitCore();
23 
rule_can_be_changed_to_strict()24     @Test public void rule_can_be_changed_to_strict() throws Throwable {
25         //when
26         Result result = runner.run(LenientByDefault.class);
27 
28         //then
29         JUnitResultAssert.assertThat(result)
30                 .succeeds(1)
31                 .fails(1, RuntimeException.class);
32     }
33 
rule_can_be_changed_to_lenient()34     @Test public void rule_can_be_changed_to_lenient() throws Throwable {
35         //when
36         Result result = runner.run(StrictByDefault.class);
37 
38         //then
39         JUnitResultAssert.assertThat(result)
40                 .succeeds(1)
41                 .fails(1, RuntimeException.class);
42     }
43 
44     public static class LenientByDefault {
45         @Rule public MockitoRule mockito = MockitoJUnit.rule().strictness(Strictness.LENIENT);
46         @Mock IMethods mock;
47 
unused_stub()48         @Test public void unused_stub() throws Throwable {
49             when(mock.simpleMethod()).thenReturn("1");
50         }
51 
unused_stub_with_strictness()52         @Test public void unused_stub_with_strictness() throws Throwable {
53             //making Mockito strict only for this test method
54             mockito.strictness(Strictness.STRICT_STUBS);
55 
56             when(mock.simpleMethod()).thenReturn("1");
57         }
58     }
59 
60     public static class StrictByDefault {
61         @Rule public MockitoRule mockito = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS);
62         @Mock IMethods mock;
63 
unused_stub()64         @Test public void unused_stub() throws Throwable {
65             when(mock.simpleMethod()).thenReturn("1");
66         }
67 
unused_stub_with_lenient()68         @Test public void unused_stub_with_lenient() throws Throwable {
69             //making Mockito lenient only for this test method
70             mockito.strictness(Strictness.LENIENT);
71 
72             when(mock.simpleMethod()).thenReturn("1");
73         }
74     }
75 }
76