question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Top level @TestConfiguration picked up by @SpringBootTest without @Import when it referenced from another module

See original GitHub issue

Main reason for @TestConfiguration is to allow having configuration without picking it up by auto scanning.

But my test with @SpringBootTest and without any @Import loads definitions from top-level class marked by @TestConfiguration.

|    |    +--- org.springframework.boot:spring-boot-starter-test: -> 1.5.3.RELEASE
|    |    |    +--- org.springframework.boot:spring-boot-test:1.5.3.RELEASE
|    |    |    \--- org.springframework:spring-test:4.3.8.RELEASE

@TestConfiguration is defined in Core module and @SpringBootTest class in module that uses Core test module via Gradle’s testCompile project (...).

Documentation says:

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-testing-spring-boot-applications-excluding-config

When placed on a top-level class, @TestConfiguration indicates that classes in src/test/java should not be picked up by scanning.

Is dependency for module via testCompile project a reason for a problem? How can I mark a class to be eligible for @Import without being auto-scanned?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
tkentcommented, Aug 16, 2019

In case somebody happens across this issue, we ran into a similiar problem to the one described by @gavenkoa.

In our case, we found that someone had mistakenly included an unnecessary @ComponentScan annotation on one of the app configuration classes. Probably just a reflex from the pre @SpringBootApplication days.

Anyway, removing the unneeded @ComponentScan resolved the issue.

4reactions
mpscheidtcommented, Mar 4, 2021

If test is configured with @SpringBootConfiguration plus @ComponentScan instead of @SpringBootApplication, then excludeFilters need to be added in order to avoid picking up @TestConfigurations without explicit @Import:

@SpringBootConfiguration
@ComponentScan(value = { "my.base.package" }, excludeFilters = {
		@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
		@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public class BootTestConfig { }
Read more comments on GitHub >

github_iconTop Results From Across the Web

46. Testing - Spring
When placed on a top-level class, @TestConfiguration indicates that classes in src/test/java should not be picked up by scanning. You can then import...
Read more >
Spring Boot: @TestConfiguration Not Overriding Bean During ...
I try to do a test with #TestConfiguration placed on a top-level class and with #Import(MyTestConfiguration.class) and without # ...
Read more >
Testing with Spring Boot's @TestConfiguration Annotation
The @TestConfiguration annotation is a useful aid for writing unit tests of components in a Spring Boot application.
Read more >
version 1.3 - Spock Framework Reference Documentation
Spock is a testing and specification framework for Java and Groovy applications. What makes it stand out from the crowd is its beautiful...
Read more >
A Quick Guide to @TestPropertySource - Baeldung
Spring brings many features to help us with testing our code. Sometimes we need to use particular configuration properties in order to set...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found