protected void handleException(final TestMethod testMethod, Throwable actualFailure) { try { final String throwableName = actualFailure.getClass().getName(); if (throwableName.equals("org.junit.internal.AssumptionViolatedException") || throwableName.startsWith("org.junit.Assume$AssumptionViolatedException")) { return; } else if (!(Boolean) Whitebox.invokeMethod(testMethod, "expectsException")) { final String className = actualFailure.getStackTrace()[0].getClassName(); final Class<?> testClassAsJavaClass = testClass.getJavaClass(); if (actualFailure instanceof NullPointerException && !testClassAsJavaClass.getName().equals(className) && !className.startsWith("java.lang") && !className.startsWith("org.powermock") && !className.startsWith("org.junit") && !new PrepareForTestExtractorImpl().isPrepared(testClassAsJavaClass, className) && !testClassAsJavaClass.isAnnotationPresent(PrepareEverythingForTest.class) && !new MockPolicyInitializerImpl(testClassAsJavaClass.isAnnotationPresent(MockPolicy.class) ? testClassAsJavaClass .getAnnotation(MockPolicy.class).value() : null).isPrepared(className)) { Whitebox.setInternalState(actualFailure, "detailMessage", "Perhaps the class " + className + " must be prepared for test?", Throwable.class); } addFailure(actualFailure); } else if ((Boolean) Whitebox.invokeMethod(testMethod, "isUnexpected", actualFailure)) { String message = "Unexpected exception, expected<" + getExpectedExceptionName(testMethod) + "> but was<" + actualFailure.getClass().getName() + ">"; addFailure(new Exception(message, actualFailure)); } } catch (Exception e) { throw new RuntimeException("PowerMock internal error: Should never throw exception at this level", e); } }
/** * Get the mock policies from a test-class. */ @SuppressWarnings("unchecked") private static Class<? extends PowerMockPolicy>[] getMockPolicies(Class<?> testClass) { Class<? extends PowerMockPolicy>[] powerMockPolicies = new Class[0]; if (testClass.isAnnotationPresent(MockPolicy.class)) { MockPolicy annotation = testClass.getAnnotation(MockPolicy.class); powerMockPolicies = annotation.value(); } return powerMockPolicies; }