1 package org.mockitousage.junitrule; 2 3 import org.junit.Rule; 4 import org.junit.Test; 5 import org.junit.runner.JUnitCore; 6 import org.junit.runner.Result; 7 import org.mockito.exceptions.base.MockitoAssertionError; 8 import org.mockito.junit.MockitoJUnit; 9 import org.mockito.junit.VerificationCollector; 10 import org.mockitousage.IMethods; 11 12 import static org.assertj.core.api.Assertions.assertThat; 13 import static org.junit.Assert.fail; 14 import static org.mockito.Mockito.*; 15 16 public class VerificationCollectorImplTest { 17 18 @Test 19 public void should_not_throw_any_exceptions_when_verifications_are_succesful() { 20 VerificationCollector collector = MockitoJUnit.collector().assertLazily(); 21 22 IMethods methods = mock(IMethods.class); 23 methods.simpleMethod(); 24 25 verify(methods).simpleMethod(); 26 collector.collectAndReport(); 27 } 28 29 @Test(expected = MockitoAssertionError.class) 30 public void should_collect_verification_failures() { 31 VerificationCollector collector = MockitoJUnit.collector().assertLazily(); 32 33 IMethods methods = mock(IMethods.class); 34 35 verify(methods).simpleMethod(); 36 collector.collectAndReport(); 37 } 38 39 @Test 40 public void should_collect_multiple_verification_failures() { 41 VerificationCollector collector = MockitoJUnit.collector().assertLazily(); 42 43 IMethods methods = mock(IMethods.class); 44 45 verify(methods).simpleMethod(); 46 verify(methods).byteReturningMethod(); 47 try { 48 collector.collectAndReport(); 49 fail(); 50 } catch (MockitoAssertionError error) { 51 assertThat(error).hasMessageContaining("1. Wanted but not invoked:"); 52 assertThat(error).hasMessageContaining("2. Wanted but not invoked:"); 53 } 54 } 55 56 @Test 57 public void should_only_collect_failures_ignore_succesful_verifications() { 58 VerificationCollector collector = MockitoJUnit.collector().assertLazily(); 59 60 IMethods methods = mock(IMethods.class); 61 62 verify(methods, never()).simpleMethod(); 63 verify(methods).byteReturningMethod(); 64 65 this.assertAtLeastOneFailure(collector); 66 } 67 68 @Test 69 public void should_continue_collecting_after_failing_verification() { 70 VerificationCollector collector = MockitoJUnit.collector().assertLazily(); 71 72 IMethods methods = mock(IMethods.class); 73 methods.simpleMethod(); 74 75 verify(methods).byteReturningMethod(); 76 verify(methods).simpleMethod(); 77 78 this.assertAtLeastOneFailure(collector); 79 } 80 81 private void assertAtLeastOneFailure(VerificationCollector collector) { 82 try { 83 collector.collectAndReport(); 84 fail(); 85 } catch (MockitoAssertionError error) { 86 assertThat(error).hasMessageContaining("1. Wanted but not invoked:"); 87 assertThat(error.getMessage()).doesNotContain("2."); 88 } 89 } 90 91 @Test 92 public void should_invoke_collector_rule_after_test() { 93 JUnitCore runner = new JUnitCore(); 94 Result result = runner.run(VerificationCollectorRuleInner.class); 95 96 assertThat(result.getFailureCount()).isEqualTo(1); 97 assertThat(result.getFailures().get(0).getMessage()).contains("1. Wanted but not invoked:"); 98 } 99 100 // This class is picked up when running a test suite using an IDE. It fails on purpose. 101 public static class VerificationCollectorRuleInner { 102 103 @Rule 104 public VerificationCollector collector = MockitoJUnit.collector(); 105 106 @Test 107 public void should_fail() { 108 IMethods methods = mock(IMethods.class); 109 110 verify(methods).simpleMethod(); 111 } 112 113 @Test 114 public void should_not_fail() { 115 IMethods methods = mock(IMethods.class); 116 methods.simpleMethod(); 117 118 verify(methods).simpleMethod(); 119 } 120 } 121 } 122