@BeforeClass protected void beforePowerMockTestClass() throws Exception { // To make sure that the mock repository is not in an incorrect state when the test begins MockRepository.clear(); if(isLoadedByPowerMockClassloader()) { final Thread thread = Thread.currentThread(); previousCl = thread.getContextClassLoader(); thread.setContextClassLoader(this.getClass().getClassLoader()); } }
/** * Must be executed after each test method. This method does the following: * <ol> * <li>Clear all injection fields (those annotated with a Mock annotation)</li> * <li>Clears the PowerMock MockRepository</li> * </ol> * * @throws Exception if something unexpected goes wrong. */ @AfterMethod protected void afterPowerMockTestMethod() throws Exception { try { clearMockFields(); } finally { MockRepository.clear(); } }
@Override public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable { injectMocksUsingAnnotationEnabler(self); try { final Object result = proceed.invoke(self, args); return result; } catch (InvocationTargetException e) { throw e.getTargetException(); } finally { if (thisMethod.isAnnotationPresent(Test.class)) { clearMockFields(); MockRepository.clear(); } } }
public static void initialize(Class<?> testClass, JavaAgentClassRegister agentClassRegister) { /* * For extra safety clear the MockitoRepository. */ MockRepository.clear(); redefineClasses(testClass, agentClassRegister); final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); new MockPolicyInitializerImpl(testClass).initialize(contextClassLoader); }
@Override public Object newInstance(Constructor constructor, Object... params) { /* * For extra safety clear the MockitoRepository on each new * instantiation of the object factory. This is good in cases where a * previous test has used e.g. PowerMock#createMock(..) to create a mock * without using this factory. That means that there's some state left in * the MockRepository that hasn't been cleared. Currently clearing the * MockRepository from any classloader will clear the previous state but * it's not certain that this is always the case. */ MockRepository.clear(); return new TestClassInstanceFactory(constructor, classLoaderFactory, params).create(); }
@Override public void run() { try { // Re-executes the policy method that might initialize mocks that // were cleared after the previous statement. // This fixes https://github.com/jayway/powermock/issues/581 mockPolicyInitializer.refreshPolicies(getClass().getClassLoader()); fNext.evaluate(); } catch (Throwable e) { SafeExceptionRethrower.safeRethrow(e); } finally { // Clear the mock repository after each test MockRepository.clear(); } } });
@Override public void evaluate() throws Throwable { Object annotationEnabler = loadAnnotationEnableIfPresent(); try { injectMocksUsingAnnotationEnabler(target, annotationEnabler); setFrameworkAgentClassRegister(); fNext.evaluate(); } finally { // Clear the mock repository after each test MockRepository.clear(); clearMockFields(target, annotationEnabler); clearFrameworkAgentClassRegister(); } }