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:
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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.
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user