private boolean hasPowerMockAnnotation(Class<?> testClass) { return isClassAnnotatedWithPowerMockAnnotation(testClass) || anyMethodInClassHasPowerMockAnnotation(testClass); }
/** * Must be executed before each test method. This method does the following: * <ol> * <li>Injects all mock fields (if they haven't been injected already)</li> * </ol> * * * * @throws Exception * If something unexpected goes wrong. */ @BeforeMethod protected void beforePowerMockTestMethod() throws Exception { injectMocks(); }
@Override public Object newInstance(Constructor constructor, Object... params) { final Object testInstance; Class<?> testClass = constructor.getDeclaringClass(); if (hasPowerMockAnnotation(testClass)) { testInstance = powerMockObjectFactory.newInstance(constructor, params); } else { testInstance = defaultObjectFactory.newInstance(constructor, params); } return testInstance; }
@ObjectFactory public IObjectFactory getObjectFactory() { return new org.powermock.modules.testng.PowerMockObjectFactory(); }
/** * 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(); } }
@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()); } }
/** * We need a special {@link IObjectFactory}. * * @return {@link PowerMockObjectFactory}. */ @ObjectFactory public IObjectFactory getObjectFactory() { return new PowerMockObjectFactory(); }