Make it easier to add auto-configuration to a test slice

Previously, an entry had to be added to spring.factories using the
name of one of the @AutoConfigure… meta-annotations on the @…Test
annotation as the key. This indirection was unnecessarily complicated.

This commit simplifies things by allowing the name of the @…Test
annotation itself to be used as the key.

Closes gh-6335
pull/6323/merge
Andy Wilkinson 8 years ago
parent 4fe28727ad
commit 3286760073

@ -76,12 +76,21 @@ TestSlice createTestSlice(Properties springFactories, ClassMetadata classMetadat
}
Set<String> getImportedAutoConfiguration(Properties springFactories, ClassMetadata classMetadata, AnnotationMetadata annotationMetadata) {
Set<String> importers = findMetaImporters(annotationMetadata)
if (annotationMetadata.isAnnotated('org.springframework.boot.autoconfigure.ImportAutoConfiguration')) {
importers.add(annotationMetadata.className)
}
importers
.collect { autoConfigurationImporter ->
StringUtils.commaDelimitedListToSet(springFactories.get(autoConfigurationImporter))
}.flatten()
}
Set<String> findMetaImporters(AnnotationMetadata annotationMetadata) {
annotationMetadata.annotationTypes
.findAll { annotationType ->
isAutoConfigurationImporter(annotationType, annotationMetadata)
}.collect { autoConfigurationImporter ->
StringUtils.commaDelimitedListToSet(springFactories.get(autoConfigurationImporter))
}.flatten()
}
}
boolean isAutoConfigurationImporter(String annotationType, AnnotationMetadata metadata) {

@ -18,6 +18,7 @@ package sample.devtools;
import java.util.Date;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
@ -28,6 +29,11 @@ import org.springframework.web.servlet.ModelAndView;
@Controller
public class MyController {
@PostConstruct
public void slowRestart() throws InterruptedException {
Thread.sleep(5000);
}
@GetMapping("/")
public ModelAndView get(HttpSession session) {
Object sessionVar = session.getAttribute("var");

@ -23,6 +23,7 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration;
import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache;
@ -62,6 +63,7 @@ import org.springframework.test.context.BootstrapWith;
@AutoConfigureCache
@AutoConfigureJson
@AutoConfigureJsonTesters
@ImportAutoConfiguration
public @interface JsonTest {
/**

@ -23,6 +23,7 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration;
import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache;
@ -70,6 +71,7 @@ import org.springframework.transaction.annotation.Transactional;
@AutoConfigureDataJpa
@AutoConfigureTestDatabase
@AutoConfigureTestEntityManager
@ImportAutoConfiguration
public @interface DataJpaTest {
/**

@ -23,6 +23,7 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration;
import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache;
@ -68,6 +69,7 @@ import org.springframework.web.client.RestTemplate;
@AutoConfigureCache
@AutoConfigureWebClient
@AutoConfigureMockRestServiceServer
@ImportAutoConfiguration
public @interface RestClientTest {
/**

@ -23,6 +23,7 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration;
import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache;
@ -75,6 +76,7 @@ import org.springframework.test.web.servlet.MockMvc;
@AutoConfigureCache
@AutoConfigureWebMvc
@AutoConfigureMockMvc
@ImportAutoConfiguration
public @interface WebMvcTest {
/**

Loading…
Cancel
Save