From 6fc6a9e80ce5119eeb9cdd0d0962e9f989ab10c2 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 18 Jul 2011 20:49:26 +0000 Subject: [PATCH] fixed @Rule execution order in SpringJUnit4ClassRunner to match standard JUnit 4 behavior (SPR-8537) --- .../junit4/SpringJUnit4ClassRunner.java | 52 +++++-------------- 1 file changed, 14 insertions(+), 38 deletions(-) diff --git a/org.springframework.test/src/main/java/org/springframework/test/context/junit4/SpringJUnit4ClassRunner.java b/org.springframework.test/src/main/java/org/springframework/test/context/junit4/SpringJUnit4ClassRunner.java index 1584fa064a8..e4967d8d54f 100644 --- a/org.springframework.test/src/main/java/org/springframework/test/context/junit4/SpringJUnit4ClassRunner.java +++ b/org.springframework.test/src/main/java/org/springframework/test/context/junit4/SpringJUnit4ClassRunner.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2011 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,6 +34,7 @@ import org.junit.runners.BlockJUnit4ClassRunner; import org.junit.runners.model.FrameworkMethod; import org.junit.runners.model.InitializationError; import org.junit.runners.model.Statement; + import org.springframework.test.annotation.ExpectedException; import org.springframework.test.annotation.ProfileValueUtils; import org.springframework.test.annotation.Repeat; @@ -96,7 +97,6 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner { * Constructs a new SpringJUnit4ClassRunner and initializes a * {@link TestContextManager} to provide Spring testing functionality to * standard JUnit tests. - * * @param clazz the test class to be run * @see #createTestContextManager(Class) */ @@ -112,7 +112,6 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner { * Creates a new {@link TestContextManager} for the supplied test class and * the configured default ContextLoader class name. * Can be overridden by subclasses. - * * @param clazz the test class to be managed * @see #getDefaultContextLoaderClassName(Class) */ @@ -132,12 +131,9 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner { * the supplied test class. The named class will be used if the test class * does not explicitly declare a ContextLoader class via the * @ContextConfiguration annotation. - *

- * The default implementation returns null, thus implying use + *

The default implementation returns null, thus implying use * of the standard default ContextLoader class name. * Can be overridden by subclasses. - *

- * * @param clazz the test class * @return null */ @@ -149,7 +145,6 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner { * Returns a description suitable for an ignored test class if the test is * disabled via @IfProfileValue at the class-level, and * otherwise delegates to the parent implementation. - * * @see ProfileValueUtils#isTestEnabledInThisEnvironment(Class) */ @Override @@ -166,7 +161,6 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner { * from running altogether, even skipping the execution of * prepareTestInstance() TestExecutionListener * methods. - * * @see ProfileValueUtils#isTestEnabledInThisEnvironment(Class) * @see org.springframework.test.annotation.IfProfileValue * @see org.springframework.test.context.TestExecutionListener @@ -185,7 +179,6 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner { * {@link RunBeforeTestClassCallbacks} statement, thus preserving the * default functionality but adding support for the Spring TestContext * Framework. - * * @see RunBeforeTestClassCallbacks */ @Override @@ -198,7 +191,6 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner { * Wraps the {@link Statement} returned by the parent implementation with a * {@link RunAfterTestClassCallbacks} statement, thus preserving the default * functionality but adding support for the Spring TestContext Framework. - * * @see RunAfterTestClassCallbacks */ @Override @@ -211,7 +203,6 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner { * Delegates to the parent implementation for creating the test instance and * then allows the {@link #getTestContextManager() TestContextManager} to * prepare the test instance before returning it. - * * @see TestContextManager#prepareTestInstance(Object) */ @Override @@ -267,8 +258,7 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner { * Augments the default JUnit behavior * {@link #withPotentialRepeat(FrameworkMethod, Object, Statement) with * potential repeats} of the entire execution chain. - *

- * Furthermore, support for timeouts has been moved down the execution chain + *

Furthermore, support for timeouts has been moved down the execution chain * in order to include execution of {@link org.junit.Before @Before} * and {@link org.junit.After @After} methods within the timed * execution. Note that this differs from the default JUnit behavior of @@ -281,8 +271,6 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner { * timeouts still differ from Spring-specific timeouts in that the former * execute in a separate thread while the latter simply execute in the main * thread (like regular tests). - *

- * * @see #possiblyExpectingExceptions(FrameworkMethod, Object, Statement) * @see #withBefores(FrameworkMethod, Object, Statement) * @see #withAfters(FrameworkMethod, Object, Statement) @@ -291,26 +279,24 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner { */ @Override protected Statement methodBlock(FrameworkMethod frameworkMethod) { - Object testInstance; try { testInstance = new ReflectiveCallable() { - @Override protected Object runReflectiveCall() throws Throwable { return createTest(); } }.run(); } - catch (Throwable e) { - return new Fail(e); + catch (Throwable ex) { + return new Fail(ex); } Statement statement = methodInvoker(frameworkMethod, testInstance); statement = possiblyExpectingExceptions(frameworkMethod, testInstance, statement); - statement = withRulesReflectively(frameworkMethod, testInstance, statement); statement = withBefores(frameworkMethod, testInstance, statement); statement = withAfters(frameworkMethod, testInstance, statement); + statement = withRulesReflectively(frameworkMethod, testInstance, statement); statement = withPotentialRepeat(frameworkMethod, testInstance, statement); statement = withPotentialTimeout(frameworkMethod, testInstance, statement); @@ -339,7 +325,6 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner { * Returns true if {@link Ignore @Ignore} is present for * the supplied {@link FrameworkMethod test method} or if the test method is * disabled via @IfProfileValue. - * * @see ProfileValueUtils#isTestEnabledInThisEnvironment(Method, Class) */ protected boolean isTestMethodIgnored(FrameworkMethod frameworkMethod) { @@ -363,19 +348,15 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner { /** * Get the exception that the supplied {@link FrameworkMethod * test method} is expected to throw. - *

- * Supports both Spring's {@link ExpectedException @ExpectedException(...)} + *

Supports both Spring's {@link ExpectedException @ExpectedException(...)} * and JUnit's {@link Test#expected() @Test(expected=...)} annotations, but * not both simultaneously. - *

- * - * @return the expected exception, or null if none was - * specified + * @return the expected exception, or null if none was specified */ protected Class getExpectedException(FrameworkMethod frameworkMethod) { Test testAnnotation = frameworkMethod.getAnnotation(Test.class); - Class junitExpectedException = testAnnotation != null - && testAnnotation.expected() != Test.None.class ? testAnnotation.expected() : null; + Class junitExpectedException = (testAnnotation != null && + testAnnotation.expected() != Test.None.class ? testAnnotation.expected() : null); ExpectedException expectedExAnn = frameworkMethod.getAnnotation(ExpectedException.class); Class springExpectedException = (expectedExAnn != null ? expectedExAnn.value() : null); @@ -399,7 +380,6 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner { * simultaneously. Returns either a {@link SpringFailOnTimeout}, a * {@link FailOnTimeout}, or the unmodified, supplied {@link Statement} as * appropriate. - * * @see #getSpringTimeout(FrameworkMethod) * @see #getJUnitTimeout(FrameworkMethod) */ @@ -431,9 +411,7 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner { /** * Retrieves the configured JUnit timeout from the {@link Test - * @Test} annotation on the supplied {@link FrameworkMethod test - * method}. - * + * @Test} annotation on the supplied {@link FrameworkMethod test method}. * @return the timeout, or 0 if none was specified. */ protected long getJUnitTimeout(FrameworkMethod frameworkMethod) { @@ -445,7 +423,6 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner { * Retrieves the configured Spring-specific timeout from the * {@link Timed @Timed} annotation on the supplied * {@link FrameworkMethod test method}. - * * @return the timeout, or 0 if none was specified. */ protected long getSpringTimeout(FrameworkMethod frameworkMethod) { @@ -458,10 +435,10 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner { * {@link RunBeforeTestMethodCallbacks} statement, thus preserving the * default functionality but adding support for the Spring TestContext * Framework. - * * @see RunBeforeTestMethodCallbacks */ @Override + @SuppressWarnings("deprecation") protected Statement withBefores(FrameworkMethod frameworkMethod, Object testInstance, Statement statement) { Statement junitBefores = super.withBefores(frameworkMethod, testInstance, statement); return new RunBeforeTestMethodCallbacks(junitBefores, testInstance, frameworkMethod.getMethod(), @@ -473,10 +450,10 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner { * {@link RunAfterTestMethodCallbacks} statement, thus preserving the * default functionality but adding support for the Spring TestContext * Framework. - * * @see RunAfterTestMethodCallbacks */ @Override + @SuppressWarnings("deprecation") protected Statement withAfters(FrameworkMethod frameworkMethod, Object testInstance, Statement statement) { Statement junitAfters = super.withAfters(frameworkMethod, testInstance, statement); return new RunAfterTestMethodCallbacks(junitAfters, testInstance, frameworkMethod.getMethod(), @@ -487,7 +464,6 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner { * Supports Spring's {@link Repeat @Repeat} annotation by returning a * {@link SpringRepeat} statement initialized with the configured repeat * count or 1 if no repeat count is configured. - * * @see SpringRepeat */ protected Statement withPotentialRepeat(FrameworkMethod frameworkMethod, Object testInstance, Statement next) {