Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@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.

like image 682
hotzen Avatar asked Oct 04 '18 15:10

hotzen


People also ask

Can we use both @SpringBootApplication and @EnableAutoConfiguration?

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.

What is the use of @EnableAutoConfiguration annotation?

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.

What is difference between @EnableAutoConfiguration and SpringBootApplication?

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.

How do I enable auto-configuration?

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.


1 Answers

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 {

    }

    ....

}
like image 75
bobmarksie Avatar answered Oct 17 '22 16:10

bobmarksie