Introduce TestContextManager cache in SpringClassRule

In order to simplify configuration of the SpringMethodRule and to ensure
that the correct TestContextManager is always retrieved for the
currently executing test class, this commit introduces a static
TestContextManager cache in SpringClassRule.

In addition, since it is not foreseen that SpringClassRule and
SpringMethodRule should be able to be subclassed, their internal methods
are now private instead of protected.

Issue: SPR-7731
This commit is contained in:
Sam Brannen
2015-05-17 17:14:09 +02:00
parent 68322fc42a
commit 973582e7df
16 changed files with 229 additions and 162 deletions

View File

@@ -43,6 +43,22 @@ import static org.junit.Assert.*;
@ContextConfiguration
public class BaseAppCtxRuleTests {
@ClassRule
public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();
@Rule
public final SpringMethodRule springMethodRule = new SpringMethodRule();
@Autowired
private String foo;
@Test
public void foo() {
assertEquals("foo", foo);
}
@Configuration
static class Config {
@@ -51,21 +67,4 @@ public class BaseAppCtxRuleTests {
return "foo";
}
}
@ClassRule
public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();
@Rule
public final SpringMethodRule springMethodRule = new SpringMethodRule(this);
@Autowired
String foo;
@Test
public void test() {
assertEquals("foo", foo);
}
}

View File

@@ -40,6 +40,6 @@ public class BasicAnnotationConfigWacSpringRuleTests extends BasicAnnotationConf
public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();
@Rule
public final SpringMethodRule springMethodRule = new SpringMethodRule(this);
public final SpringMethodRule springMethodRule = new SpringMethodRule();
}

View File

@@ -38,7 +38,7 @@ public class BeforeAndAfterTransactionAnnotationSpringRuleTests extends BeforeAn
public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();
@Rule
public final SpringMethodRule springMethodRule = new SpringMethodRule(this);
public final SpringMethodRule springMethodRule = new SpringMethodRule();
// All tests are in superclass.

View File

@@ -38,7 +38,7 @@ public class ClassLevelDisabledSpringRuleTests extends ClassLevelDisabledSpringR
public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();
@Rule
public final SpringMethodRule springMethodRule = new SpringMethodRule(this);
public final SpringMethodRule springMethodRule = new SpringMethodRule();
// All tests are in superclass.

View File

@@ -38,7 +38,7 @@ public class EnabledAndIgnoredSpringRuleTests extends EnabledAndIgnoredSpringRun
public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();
@Rule
public final SpringMethodRule springMethodRule = new SpringMethodRule(this);
public final SpringMethodRule springMethodRule = new SpringMethodRule();
// All tests are in superclass.

View File

@@ -76,7 +76,7 @@ public class FailingBeforeAndAfterMethodsSpringRuleTests extends FailingBeforeAn
public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();
@Rule
public final SpringMethodRule springMethodRule = new SpringMethodRule(this);
public final SpringMethodRule springMethodRule = new SpringMethodRule();
@Test
@@ -119,7 +119,7 @@ public class FailingBeforeAndAfterMethodsSpringRuleTests extends FailingBeforeAn
public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();
@Rule
public final SpringMethodRule springMethodRule = new SpringMethodRule(this);
public final SpringMethodRule springMethodRule = new SpringMethodRule();
@Test
@@ -142,7 +142,7 @@ public class FailingBeforeAndAfterMethodsSpringRuleTests extends FailingBeforeAn
public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();
@Rule
public final SpringMethodRule springMethodRule = new SpringMethodRule(this);
public final SpringMethodRule springMethodRule = new SpringMethodRule();
@Test

View File

@@ -55,7 +55,7 @@ public class ParameterizedSpringRuleTests {
public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();
@Rule
public final SpringMethodRule springMethodRule = new SpringMethodRule(this);
public final SpringMethodRule springMethodRule = new SpringMethodRule();
@Autowired
private ApplicationContext applicationContext;

View File

@@ -47,7 +47,7 @@ public class ProgrammaticTxMgmtSpringRuleTests extends ProgrammaticTxMgmtTests {
public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();
@Rule
public final SpringMethodRule springMethodRule = new SpringMethodRule(this);
public final SpringMethodRule springMethodRule = new SpringMethodRule();
// All tests are in superclass.

View File

@@ -76,7 +76,7 @@ public class RepeatedSpringRuleTests extends RepeatedSpringRunnerTests {
public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();
@Rule
public final SpringMethodRule springMethodRule = new SpringMethodRule(this);
public final SpringMethodRule springMethodRule = new SpringMethodRule();
protected void incrementInvocationCount() throws IOException {

View File

@@ -16,14 +16,40 @@
package org.springframework.test.context.junit4.rules;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import static org.junit.Assert.*;
/**
* Subclass #1 of {@link BaseAppCtxRuleTests}.
*
* @author Sam Brannen
* @since 4.2
*/
@ContextConfiguration
public class Subclass1AppCtxRuleTests extends BaseAppCtxRuleTests {
// All tests and config are in superclass.
@Autowired
private String bar;
@Test
public void bar() {
assertEquals("bar", bar);
}
@Configuration
static class Config {
@Bean
public String bar() {
return "bar";
}
}
}

View File

@@ -16,14 +16,40 @@
package org.springframework.test.context.junit4.rules;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import static org.junit.Assert.*;
/**
* Subclass #2 of {@link BaseAppCtxRuleTests}.
*
* @author Sam Brannen
* @since 4.2
*/
@ContextConfiguration
public class Subclass2AppCtxRuleTests extends BaseAppCtxRuleTests {
// All tests and config are in superclass.
@Autowired
private String baz;
@Test
public void baz() {
assertEquals("baz", baz);
}
@Configuration
static class Config {
@Bean
public String baz() {
return "baz";
}
}
}

View File

@@ -58,7 +58,7 @@ public class TimedSpringRuleTests extends TimedSpringRunnerTests {
public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();
@Rule
public final SpringMethodRule springMethodRule = new SpringMethodRule(this);
public final SpringMethodRule springMethodRule = new SpringMethodRule();
/**

View File

@@ -45,7 +45,7 @@ public class TimedTransactionalSpringRuleTests extends TimedTransactionalSpringR
public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();
@Rule
public final SpringMethodRule springMethodRule = new SpringMethodRule(this);
public final SpringMethodRule springMethodRule = new SpringMethodRule();
@Rule
public Timeout timeout = Timeout.builder().withTimeout(10, TimeUnit.SECONDS).build();

View File

@@ -49,7 +49,7 @@ public class TransactionalSqlScriptsSpringRuleTests extends TransactionalSqlScri
public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();
@Rule
public final SpringMethodRule springMethodRule = new SpringMethodRule(this);
public final SpringMethodRule springMethodRule = new SpringMethodRule();
@Rule
public Timeout timeout = Timeout.builder().withTimeout(10, TimeUnit.SECONDS).build();