• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2007 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 package org.mockito.exceptions.base;
6 
7 import org.mockito.internal.exceptions.stacktrace.ConditionalStackTraceFilter;
8 
9 /**
10  * Base class for verification errors emitted by Mockito.
11  * Verification errors are triggered by "verify" methods,
12  * for example {@link org.mockito.Mockito#verify(Object)} or {@link org.mockito.Mockito#verifyNoMoreInteractions(Object...)}.
13  * All error classes that inherit from this class will have the stack trace filtered.
14  * Filtering removes Mockito internal stack trace elements to provide clean stack traces and improve productivity.
15  * <p>
16  * The stack trace is filtered from mockito calls if you are using {@link #getStackTrace()}.
17  * For debugging purpose though you can still access the full stacktrace using {@link #getUnfilteredStackTrace()}.
18  * However note that other calls related to the stackTrace will refer to the filter stacktrace.
19  * <p>
20  * Advanced users and framework integrators can control stack trace filtering behavior
21  * via {@link org.mockito.plugins.StackTraceCleanerProvider} classpath plugin.
22  */
23 public class MockitoAssertionError extends AssertionError {
24 
25     private static final long serialVersionUID = 1L;
26     private final StackTraceElement[] unfilteredStackTrace;
27 
MockitoAssertionError(String message)28     public MockitoAssertionError(String message) {
29         super(message);
30 
31         unfilteredStackTrace = getStackTrace();
32 
33         ConditionalStackTraceFilter filter = new ConditionalStackTraceFilter();
34         filter.filter(this);
35     }
36 
37     /**
38      * Creates a copy of the given assertion error with the custom failure message prepended.
39      * @param error The assertion error to copy
40      * @param message The custom message to prepend
41      * @since 2.1.0
42      */
MockitoAssertionError(MockitoAssertionError error, String message)43     public MockitoAssertionError(MockitoAssertionError error, String message) {
44         super(message + "\n" + error.getMessage());
45         super.setStackTrace(error.getStackTrace());
46         unfilteredStackTrace = error.getUnfilteredStackTrace();
47     }
48 
49     /**
50      * Creates a copy of the given assertion error with the custom failure message prepended.
51      * @param error The assertion error to copy
52      * @param message The custom message to prepend
53      * @since 3.3.13
54      */
MockitoAssertionError(AssertionError error, String message)55     public MockitoAssertionError(AssertionError error, String message) {
56         super(message + "\n" + error.getMessage());
57         unfilteredStackTrace = error.getStackTrace();
58         super.setStackTrace(unfilteredStackTrace);
59     }
60 
getUnfilteredStackTrace()61     public StackTraceElement[] getUnfilteredStackTrace() {
62         return unfilteredStackTrace;
63     }
64 }
65