private boolean isSameMockedClass(@Nullable Object mock1, @Nullable Object mock2) { if (mock1 == mock2) { return true; } if (mock1 != null && mock2 != null) { Class<?> mockedClass1 = mock1.getClass(); Class<?> mockedClass2 = mock2.getClass(); if (mockedClass1 == mockedClass2 || TestRun.getExecutingTest() .isInvokedInstanceEquivalentToCapturedInstance(mock1, mock2)) { return true; } return TestRun.mockFixture().areCapturedClasses(mockedClass1, mockedClass2); } return false; }
@Override boolean isToExecuteRealImplementation(@Nullable Object instance) { return instance != null && !TestRun.mockFixture().isInstanceOfMockedClass(instance); }
/** * Discards any mocks set up for the specified classes that are currently in effect, for all test * scopes: the current test method (if any), the current test (which starts with the first * "before" method and continues until the last "after" method), the current test class (which * includes all code from the first "before class" method to the last "after class" method), and * the current test suite. * * <p>Notice that if one of the given real classes has a mock class applied at the level of the * test class, calling this method would negate the application of that mock class. JMockit will * automatically restore classes mocked by a test at the end of its execution, as well as all * classes mocked for the test class as a whole (through a "before class" method or an * {@code @UsingMocksAndStubs} annotation) before the first test in the next test class is * executed. * * @param realClasses one or more real classes from production code, which may have mocked methods */ public static void tearDownMocks(Class<?>... realClasses) { Set<Class<?>> classesToRestore = new HashSet<Class<?>>(); Collections.addAll(classesToRestore, realClasses); TestRun.mockFixture().restoreAndRemoveRedefinedClasses(classesToRestore); }
/** * Discards any mocks currently in effect, for all test scopes: the current test method (if any), * the current test (which starts with the first "before" method and continues until the last * "after" method), the current test class (which includes all code from the first "before class" * method to the last "after class" method), and the current test suite. * * <p>Notice that a call to this method will tear down <em>all</em> mock classes that were applied * through use of the Mockups API that are still in effect, as well as any mock classes or stubs * applied to the current test class through {@code @UsingMocksAndStubs}. In other words, it would * effectively prevent mocks to be set up at the test class and test suite levels. So, use it only * if necessary and if it won't discard mock classes that should remain in effect. Consider using * {@link #tearDownMocks(Class...)} instead, which lets you restrict the set of real classes to be * restored. * * <p>JMockit will automatically restore classes mocked by a test at the end of its execution, as * well as all classes mocked for the test class as a whole (through a "before class" method or an * {@code @UsingMocksAndStubs} annotation) before the first test in the next test class is * executed. * * @see <a * href="http://code.google.com/p/jmockit/source/browse/trunk/main/test/mockit/MockAnnotationsTest.java#450"> * Example</a> */ public static void tearDownMocks() { TestRun.mockFixture().restoreAndRemoveRedefinedClasses(null); TestRun.getMockClasses().getRegularMocks().discardInstances(); }