• 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.debugging.LocationImpl;
15 import org.mockito.internal.invocation.InvocationBuilder;
16 import org.mockito.internal.invocation.InvocationImpl;
17 import org.mockito.internal.invocation.InvocationMatcher;
18 import org.mockito.internal.invocation.SerializableMethod;
19 import org.mockito.internal.invocation.realmethod.RealMethod;
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 InvocationImpl(mock(type), new SerializableMethod(type.getMethod(methodName,
69                 types)), args, 1, null, new LocationImpl());
70     }
71 
invocationOf(Class<?> type, String methodName, RealMethod realMethod)72     protected static Invocation invocationOf(Class<?> type, String methodName, RealMethod realMethod) throws NoSuchMethodException {
73         return new InvocationImpl(new Object(), new SerializableMethod(type.getMethod(methodName,
74                 new Class<?>[0])), new Object[0], 1, realMethod, new LocationImpl());
75     }
76 
invocationAt(String location)77     protected static Invocation invocationAt(String location) {
78         return new InvocationBuilder().location(location).toInvocation();
79     }
80 
invocationMatcherAt(String location)81     protected static InvocationMatcher invocationMatcherAt(String location) {
82         return new InvocationBuilder().location(location).toInvocationMatcher();
83     }
84 
getStackTrace(Throwable e)85     protected String getStackTrace(Throwable e) {
86         ByteArrayOutputStream out = new ByteArrayOutputStream();
87         e.printStackTrace(new PrintStream(out));
88         try {
89             out.close();
90         } catch (IOException ex) {}
91         return out.toString();
92     }
93 
94     /**
95      * Filters out unwanted line numbers from provided stack trace String.
96      * This is useful for writing assertions for exception messages that contain line numbers.
97      *
98      * For example it turns:
99      * blah blah (UnusedStubsExceptionMessageTest.java:27)
100      * into:
101      * blah blah (UnusedStubsExceptionMessageTest.java:0)
102      */
filterLineNo(String stackTrace)103     public static String filterLineNo(String stackTrace) {
104         return stackTrace.replaceAll("(\\((\\w+\\.java):(\\d)+\\))", "($2:0)");
105     }
106 }
107