1 /*
2  * Copyright (c) 2017 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 package org.mockito.internal.verification;
6 
7 import org.junit.Before;
8 import org.junit.Test;
9 import org.mockito.Mock;
10 import org.mockito.exceptions.base.MockitoAssertionError;
11 import org.mockito.internal.verification.api.VerificationData;
12 import org.mockito.verification.VerificationMode;
13 
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.fail;
16 import static org.mockito.Mockito.doThrow;
17 import static org.mockito.Mockito.verify;
18 import static org.mockito.MockitoAnnotations.initMocks;
19 
20 public class DescriptionTest {
21 
22     @Mock
23     private VerificationMode mockVerificationMode;
24 
25     @Mock
26     private VerificationData mockVerificationData;
27 
28     @Before
setUp()29     public void setUp() {
30         initMocks(this);
31     }
32 
33     /**
34      * Test of verify method, of class Description. This test validates that the custom message is prepended to the
35      * error message when verification fails.
36      */
37     @Test
verification_failure_should_prepend_expected_message()38     public void verification_failure_should_prepend_expected_message() {
39         String failureMessage = "message should be prepended to the original message";
40         String exceptionMessage = "original error message";
41         String expectedResult = failureMessage + "\n" + exceptionMessage;
42         MockitoAssertionError error = new MockitoAssertionError(exceptionMessage);
43         doThrow(error).when(mockVerificationMode).verify(mockVerificationData);
44 
45         Description instance = new Description(mockVerificationMode, failureMessage);
46 
47         try {
48             instance.verify(mockVerificationData);
49             verify(mockVerificationMode).verify(mockVerificationData);
50             fail("Should not have made it this far");
51 
52         } catch (MockitoAssertionError e) {
53             assertEquals(expectedResult, e.getMessage());
54         }
55     }
56 }
57