1/*
2 * Copyright (c) 2016 Mockito contributors
3 * This program is made available under the terms of the MIT License.
4 */
5package org.mockito.internal.verification;
6
7import org.mockito.exceptions.base.MockitoAssertionError;
8import org.mockito.internal.verification.api.VerificationData;
9import org.mockito.verification.VerificationMode;
10
11/**
12 * Description verification mode wraps an existing verification mode and prepends
13 * a custom message to the assertion error if verification fails.
14 * @author Geoff.Schoeman
15 * @since 2.1.0
16 */
17public class Description implements VerificationMode {
18
19    private final VerificationMode verification;
20    private final String description;
21
22    /**
23     * Constructs a verification mode which wraps the given verification mode.
24     * @param verification The implementation to use for verification
25     * @param description The failure message to prepend if verification fails
26     */
27    public Description(VerificationMode verification, String description) {
28        this.verification = verification;
29        this.description = description;
30    }
31
32    /**
33     * Performs verification using the wrapped verification mode implementation.
34     * Prepends the custom failure message if verification fails.
35     * @param data the data to be verified
36     */
37    @Override
38    public void verify(VerificationData data) {
39        try {
40            verification.verify(data);
41
42        } catch (MockitoAssertionError e) {
43            throw new MockitoAssertionError(e, description);
44        }
45    }
46
47    @Override
48    public VerificationMode description(String description) {
49        return VerificationModeFactory.description(this, description);
50    }
51}
52