GH-725 - Fix test context cache key contributions.
Both ModuleContextCustomizer and ModuleTypeExcludeFilter contribute to the calculation of the context configuration which the Spring Test Context Framework uses to decide whether it's necessary to create new ApplicationContext instances. Both of them previously used a Supplier<ModuleTestExecution> to calculate equals(…) and hashCode() which -- by definition -- does not result in the same result even when created with identical input. We now rather use the source class instance eventually backing the ModuleTestExecution, as that is the internal cache key in turn.
This commit is contained in:
@@ -62,9 +62,12 @@ class ModuleContextCustomizerFactory implements ContextCustomizerFactory {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ModuleContextCustomizer.class);
|
||||
|
||||
private final Supplier<ModuleTestExecution> execution;
|
||||
private final Class<?> source;
|
||||
|
||||
ModuleContextCustomizer(Class<?> testClass) {
|
||||
|
||||
private ModuleContextCustomizer(Class<?> testClass) {
|
||||
this.execution = ModuleTestExecution.of(testClass);
|
||||
this.source = testClass;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -143,6 +146,32 @@ class ModuleContextCustomizerFactory implements ContextCustomizerFactory {
|
||||
LOGGER.info("");
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(obj instanceof ModuleContextCustomizer that)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return Objects.equals(this.source, that.source);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hashCode(source);
|
||||
}
|
||||
|
||||
private static void logHeadline(String headline) {
|
||||
logHeadline(headline, () -> {});
|
||||
}
|
||||
@@ -153,33 +182,6 @@ class ModuleContextCustomizerFactory implements ContextCustomizerFactory {
|
||||
LOGGER.info(headline);
|
||||
additional.run();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(obj instanceof ModuleContextCustomizer that)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return Objects.equals(execution, that.execution);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(execution);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -22,6 +22,7 @@ import java.util.function.Supplier;
|
||||
import org.springframework.boot.context.TypeExcludeFilter;
|
||||
import org.springframework.core.type.classreading.MetadataReader;
|
||||
import org.springframework.core.type.classreading.MetadataReaderFactory;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* @author Oliver Drotbohm
|
||||
@@ -29,9 +30,14 @@ import org.springframework.core.type.classreading.MetadataReaderFactory;
|
||||
class ModuleTypeExcludeFilter extends TypeExcludeFilter {
|
||||
|
||||
private final Supplier<ModuleTestExecution> execution;
|
||||
private final Class<?> source;
|
||||
|
||||
public ModuleTypeExcludeFilter(Class<?> testClass) {
|
||||
|
||||
Assert.notNull(testClass, "Test class must not be null!");
|
||||
|
||||
this.execution = ModuleTestExecution.of(testClass);
|
||||
this.source = testClass;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -58,7 +64,7 @@ class ModuleTypeExcludeFilter extends TypeExcludeFilter {
|
||||
return false;
|
||||
}
|
||||
|
||||
return Objects.equals(execution, that.execution);
|
||||
return Objects.equals(source, that.source);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -67,6 +73,6 @@ class ModuleTypeExcludeFilter extends TypeExcludeFilter {
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(execution);
|
||||
return Objects.hash(source);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user