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