1 /* 2 * Copyright (c) 2016 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 package org.mockito.junit; 6 7 import org.junit.rules.TestRule; 8 import org.mockito.exceptions.base.MockitoAssertionError; 9 10 /** 11 * Use this rule in order to collect multiple verification failures and report at once. 12 * This new API in incubating - let us know if you find this feature useful. 13 * Should it be turned on by default with Mockito JUnit Rule? 14 * <p> 15 * Although {@code VerificationCollector} is a JUnit Rule, it does not necessarily have to be used as a Test Rule 16 * - see {@link #collectAndReport()}. 17 * <p> 18 * In the example below, the verification failure thrown by {@code byteReturningMethod()} does not block 19 * verifying against the {@code simpleMethod()}. After the test is run, a report is generated stating all 20 * collect verification failures. 21 * 22 * <pre class="code"><code class="java"> 23 * @Rule 24 * public VerificationCollector collector = MockitoJUnit.collector(); 25 * 26 * @Test 27 * public void should_fail() { 28 * IMethods methods = mock(IMethods.class); 29 * 30 * verify(methods).byteReturningMethod(); 31 * verify(methods).simpleMethod(); 32 * } 33 * </code></pre> 34 * 35 * @see org.mockito.Mockito#verify(Object) 36 * @see org.mockito.Mockito#verify(Object, org.mockito.verification.VerificationMode) 37 * @since 2.1.0 38 */ 39 public interface VerificationCollector extends TestRule { 40 41 /** 42 * Collect all lazily verified behaviour. If there were failed verifications, it will 43 * throw a MockitoAssertionError containing all messages indicating the failed verifications. 44 * <p> 45 * Normally, users don't need to call this method because it is automatically invoked when test finishes 46 * (part of the JUnit Rule behavior). 47 * However, in some circumstances and edge cases, it might be useful to collect and report verification 48 * errors in the middle of the test (for example: some scenario tests or during debugging). 49 * 50 * <pre class="code"><code class="java"> 51 * @Rule 52 * public VerificationCollector collector = MockitoJUnit.collector(); 53 * 54 * @Test 55 * public void should_fail() { 56 * IMethods methods = mock(IMethods.class); 57 * 58 * verify(methods).byteReturningMethod(); 59 * verify(methods).simpleMethod(); 60 * 61 * //report all verification errors now: 62 * collector.collectAndReport(); 63 * 64 * //some other test code 65 * } 66 * </code></pre> 67 * 68 * @throws MockitoAssertionError If there were failed verifications 69 * @since 2.1.0 70 */ collectAndReport()71 void collectAndReport() throws MockitoAssertionError; 72 73 /** 74 * Enforce all verifications are performed lazily. This method is automatically called when 75 * used as JUnitRule and normally users don't need to use it. 76 * <p> 77 * You should only use this method if you are using a VerificationCollector 78 * inside a method where only this method should be verified lazily. The other methods can 79 * still be verified directly. 80 * 81 * <pre class="code"><code class="java"> 82 * @Test 83 * public void should_verify_lazily() { 84 * VerificationCollector collector = MockitoJUnit.collector().assertLazily(); 85 * 86 * verify(methods).byteReturningMethod(); 87 * verify(methods).simpleMethod(); 88 * 89 * collector.collectAndReport(); 90 * } 91 * </code></pre> 92 * 93 * @return this 94 * @since 2.1.0 95 */ assertLazily()96 VerificationCollector assertLazily(); 97 } 98