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