Spring-boot: Add excludeAutoConfiguration to @SpringBootTest

Created on 12 Mar 2017  路  9Comments  路  Source: spring-projects/spring-boot

When using @DataMongoTest and flapdoodle, it's a snap to switch it on or off based on the excludeAutoConfiguration parameter.

When running an end-to-end test using Selenium and @SpringBootTest, there is no such option to disable flapdoodle. I tried @ImportAutoConfiguration#exclude), but it didn't work.

So having the means to disable autoconfigs from this test annotation would be quite handy.

duplicate

Most helpful comment

Ahh, OK. That makes sense. The @ImportAutoConfiguration excludes are only applied to those classes that would be imported by _that_ annotation. They have no effect on the @EnableAutoConfiguration annotation (via @SpringBootApplication) on LearningSpringBootApplication.

It might be nice if excludes were cumulative, but that's not currently the case.

The other annotations that offer exclude also have @OverrideAutoConfiguration(enabled = false). This means that full auto-configuration never gets applied for them.

I think the easiest work-around for now is to use a spring.autoconfigure.exclude Environment property:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties="spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration")

All 9 comments

@SpringBootTest is not part of the spring-boot-test-autoconfigure project and as such it shouldn't have any knowledge of auto-configuration.

Can you share the Selenium sample so we can work out why @ImportAutoConfiguration(exclude=...) isn't working?

If you clone https://github.com/gregturn/flapdoodle-and-selenium and then run EndToEndTests, you'll find that despite having @ImportAutoConfiguration(exclude = EmbeddedMongoAutoConfiguration.class), it will still run the test case using embedded MongoDB.

Ahh, OK. That makes sense. The @ImportAutoConfiguration excludes are only applied to those classes that would be imported by _that_ annotation. They have no effect on the @EnableAutoConfiguration annotation (via @SpringBootApplication) on LearningSpringBootApplication.

It might be nice if excludes were cumulative, but that's not currently the case.

The other annotations that offer exclude also have @OverrideAutoConfiguration(enabled = false). This means that full auto-configuration never gets applied for them.

I think the easiest work-around for now is to use a spring.autoconfigure.exclude Environment property:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties="spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration")

Possibly somewhat related #12586

What to do if facing similar issues when using @JsonTest. Want to exclude auto-configuration of a Component.

@dhirenmudgil @JsonTest has an excludeAutoConfiguration property. Please ask questions on StackOverflow. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements.

@snicoll I posted it here because the same issue is with @JsonTest and excludeAutoConfiguration is not working because of above mentioned reason

The @ImportAutoConfiguration excludes are only applied to those classes that would be imported by that annotation. They have no effect on the @EnableAutoConfiguration annotation (via @SpringBootApplication) on LearningSpringBootApplication.

And like @SpringBootTest it does not take properties.

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties="spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration")

Duplicate of #12586

@philwebb another workaround might be @TestPropertySource from spring 4.1:

@TestPropertySource(properties = "spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration")

Was this page helpful?
0 / 5 - 0 ratings