1 package org.mockito.internal.junit; 2 3 import org.junit.Rule; 4 import org.junit.Test; 5 import org.mockito.Mock; 6 import org.mockito.Mockito; 7 import org.mockito.exceptions.misusing.UnfinishedStubbingException; 8 import org.mockito.junit.MockitoJUnit; 9 import org.mockitousage.IMethods; 10 import org.mockitoutil.SafeJUnitRule; 11 12 import static org.junit.Assert.assertTrue; 13 import static org.mockito.Mockito.mockingDetails; 14 import static org.mockito.Mockito.when; 15 16 public class JUnitRuleTest { 17 18 @Rule public SafeJUnitRule rule = new SafeJUnitRule(MockitoJUnit.rule()); 19 @Mock IMethods mock; 20 21 @Test public void injects_into_test_case() throws Throwable { 22 assertTrue(mockingDetails(mock).isMock()); 23 } 24 25 @Test 26 public void rethrows_exception() throws Throwable { 27 rule.expectFailure(RuntimeException.class, "foo"); 28 throw new RuntimeException("foo"); 29 } 30 31 @Test 32 public void detects_invalid_mockito_usage_on_success() throws Throwable { 33 rule.expectFailure(UnfinishedStubbingException.class); 34 when(mock.simpleMethod()); 35 } 36 37 @Test 38 public void does_not_check_invalid_mockito_usage_on_failure() throws Throwable { 39 //This intended behavior is questionable 40 //However, it was like that since the beginning of JUnit rule support 41 //Users never questioned this behavior. Hence, let's stick to it unless we have more data 42 rule.expectFailure(RuntimeException.class, "foo"); 43 44 Mockito.when(mock.simpleMethod()); // <--- unfinished stubbing 45 throw new RuntimeException("foo"); // <--- some failure 46 } 47 } 48