Search code examples
spring-bootspring-boot-test

@EnableAutoConfiguration on AbstractIntegrationTest possible?


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.


Solution

  • 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 {
    
        }
    
        ....
    
    }