• 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.mockitoutil;
6 
7 import static org.mockito.Mockito.mock;
8 
9 import java.io.ByteArrayOutputStream;
10 import java.io.IOException;
11 import java.io.PrintStream;
12 
13 import org.assertj.core.api.Condition;
14 import org.junit.After;
15 import org.junit.Before;
16 import org.mockito.MockitoAnnotations;
17 import org.mockito.StateMaster;
18 import org.mockito.internal.MockitoCore;
19 import org.mockito.internal.configuration.ConfigurationAccess;
20 import org.mockito.internal.debugging.LocationImpl;
21 import org.mockito.internal.invocation.InterceptedInvocation;
22 import org.mockito.internal.invocation.InvocationBuilder;
23 import org.mockito.internal.invocation.InvocationMatcher;
24 import org.mockito.internal.invocation.SerializableMethod;
25 import org.mockito.internal.invocation.mockref.MockStrongReference;
26 import org.mockito.invocation.Invocation;
27 
28 /**
29  * the easiest way to make sure that tests clean up invalid state is to require
30  * valid state for all tests.
31  */
32 public class TestBase {
33 
34     /**
35      * Condition to be used with AssertJ
36      */
hasMessageContaining(final String substring)37     public static Condition<Throwable> hasMessageContaining(final String substring) {
38         return new Condition<Throwable>() {
39             @Override
40             public boolean matches(Throwable e) {
41                 return e.getMessage().contains(substring);
42             }
43         };
44     }
45 
46     @After
47     public void cleanUpConfigInAnyCase() {
48         ConfigurationAccess.getConfig().overrideCleansStackTrace(false);
49         ConfigurationAccess.getConfig().overrideDefaultAnswer(null);
50         StateMaster state = new StateMaster();
51         // catch any invalid state left over after test case run
52         // this way we can catch early if some Mockito operations leave weird state afterwards
53         state.validate();
54         // reset the state, especially, reset any ongoing stubbing for correct error messages of
55         // tests that assert unhappy paths
56         state.reset();
57     }
58 
59     @Before
60     public void init() {
61         MockitoAnnotations.openMocks(this);
62     }
63 
64     public static void makeStackTracesClean() {
65         ConfigurationAccess.getConfig().overrideCleansStackTrace(true);
66     }
67 
68     public void resetState() {
69         new StateMaster().reset();
70     }
71 
72     public static Invocation getLastInvocation() {
73         return new MockitoCore().getLastInvocation();
74     }
75 
76     protected static Invocation invocationOf(Class<?> type, String methodName, Object... args)
77             throws NoSuchMethodException {
78         Class<?>[] types = new Class<?>[args.length];
79         for (int i = 0; i < args.length; i++) {
80             types[i] = args[i].getClass();
81         }
82         return new InterceptedInvocation(
83                 new MockStrongReference<Object>(mock(type), false),
84                 new SerializableMethod(type.getMethod(methodName, types)),
85                 args,
86                 InterceptedInvocation.NO_OP,
87                 new LocationImpl(),
88                 1);
89     }
90 
91     protected static Invocation invocationAt(String location) {
92         return new InvocationBuilder().location(location).toInvocation();
93     }
94 
95     protected static InvocationMatcher invocationMatcherAt(String location) {
96         return new InvocationBuilder().location(location).toInvocationMatcher();
97     }
98 
99     protected String getStackTrace(Throwable e) {
100         ByteArrayOutputStream out = new ByteArrayOutputStream();
101         e.printStackTrace(new PrintStream(out));
102         try {
103             out.close();
104         } catch (IOException ex) {
105         }
106         return out.toString();
107     }
108 
109     /**
110      * Filters out unwanted line numbers from provided stack trace String.
111      * This is useful for writing assertions for exception messages that contain line numbers.
112      *
113      * For example it turns:
114      * blah blah (UnusedStubsExceptionMessageTest.java:27)
115      * into:
116      * blah blah (UnusedStubsExceptionMessageTest.java:0)
117      */
118     public static String filterLineNo(String stackTrace) {
119         return stackTrace.replaceAll("(\\((\\w+\\.java):(\\d)+\\))", "($2:0)");
120     }
121 
122     /**
123      * Filters out hashCode from the text. Useful for writing assertions that contain the String representation of mock objects
124      * @param text to filter
125      * @return filtered text
126      */
127     public static String filterHashCode(String text) {
128         return text.replaceAll("hashCode: (\\d)+\\.", "hashCode: xxx.");
129     }
130 }
131