Having lots of Integration-Test Implementations like this:
// no @Annotations at all
class SomeIntegrationTest extends AbstractIntegrationTest {
...
}
using (Spring Boot 1.5, JUnit 5)
@SpringBootTest(classes = {CoreConfiguration.class, RestTemplateAutoConfiguration.class, JacksonAutoConfiguration.class})
@ExtendWith(SpringExtension.class)
@AutoConfigureMockMvc
@Transactional
public abstract class AbstractIntegrationTest {
...
}
this is always failing with
org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'javax.persistence.EntityManagerFactory' available
unless I annotate every IntegrationTest-Implementation with
@EnableAutoConfiguration
class SomeIntegrationTest extends AbstractIntegrationTest {
...
}
I wonder why I cannot @EnableAutoConfiguration
the AbstractIntegrationTest
and be done with it.
(When doing so, it fails with IllegalArgumentException: No auto-configuration attributes found. Is package.SomeIntegrationTest annotated with EnableAutoConfiguration?
)
Our normal Apps look like this:
@SpringBootApplication
@Import({CoreConfiguration.class, OtherConfiguration.class})
public class WebApp {
here the @SpringBootApplication
obviously implies @EnableAutoConfiguration
but I would like to avoid annotating each and every *IntegrationTest
with this and instead configure it once on the AbstractIntegrationTest
.
Is this fighting against spring-boot in any way or is there some way to achieve this? Thanks.
You don't need to add it manually, spring will add it internally for you based on the annotation you provide. Actually the @SpringBootApplication annotation is equivalent to using @Configuration , @EnableAutoConfiguration and @ComponentScan with their default attributes.
The @EnableAutoConfiguration annotation enables Spring Boot to auto-configure the application context. Therefore, it automatically creates and registers beans based on both the included jar files in the classpath and the beans defined by us.
SpringBootApplication combines of 3 annotations: @Configuration, used for Java-based configuration on Spring framework, @ComponentScan to enable component scanning of components, and @EnableAutoConfiguration itself, which is used to allow for auto-configuration in Spring Boot application.
Annotation Type EnableAutoConfiguration. Enable auto-configuration of the Spring Application Context, attempting to guess and configure beans that you are likely to need. Auto-configuration classes are usually applied based on your classpath and what beans you have defined. For example, if you have tomcat-embedded.
You could create update your AbstractIntegrationTest
abstract class to have a small inner configuration class e.g. TestConfiguration
which is loaded using the @Import(TestConfiguration.class)
annotation.
@SpringBootTest(classes = {CoreConfiguration.class, RestTemplateAutoConfiguration.class, JacksonAutoConfiguration.class})
@ExtendWith(SpringExtension.class)
@AutoConfigureMockMvc
@Transactional
@Import(AbstractIntegrationTest.TestConfiguration.class) // <---- import the configuration
public abstract class AbstractIntegrationTest {
@EnableAutoConfiguration
// Any other applicable annotations e.g. @EntityScan
static class TestConfiguration {
}
....
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With