Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What is the difference between @TestConfiguration and @Configuration in Spring Boot tests? When is a @TestConfiguration class picked up automatically, and when must you @Import it?

@TestConfiguration declares extra or replacement beans for tests, and Spring Boot's type exclude filter keeps it out of component scanning so it never leaks into the application or into other tests. A static nested @TestConfiguration class in a test class is picked up automatically and added on top of the application's configuration, while a top-level @TestConfiguration class must be brought in with @Import. A nested @Configuration class behaves very differently: it replaces the automatic detection of the main configuration.

Why this question is asked

Test configuration is how we swap a real clock for a fixed one, a payment client for a fake, or add a helper bean that only tests need. The annotations look almost the same, but a wrong choice can quietly start a context without our application, leak test beans into production scanning, or create a new application context for every test class. Interviewers ask this to see whether we know how Spring Boot finds configuration in tests. In this answer, we will recap how @SpringBootTest finds its configuration, compare the two annotations, look at nested and top-level classes, and see how they affect the context cache.

1A Quick Recap: How @SpringBootTest Finds Configuration

A @SpringBootTest needs a starting point for its application context. If we do not name classes explicitly, Spring Boot first looks for nested @Configuration classes in the test class. If it finds none, it searches upward through the packages of the test until it finds a class annotated with @SpringBootConfiguration, which is normally our @SpringBootApplication class. That class turns on component scanning and auto-configuration, so the test gets the same beans as the application.

Component scanning, the step where Spring finds classes annotated with @Component, @Service, @Configuration and so on, would also find classes in src/test/java if they sit in a scanned package, because the test classpath includes them. That is where the special treatment of @TestConfiguration comes in.

2What Makes @TestConfiguration Different

@TestConfiguration is itself a @Configuration, so inside it we declare @Bean methods exactly as usual. The difference is how it is found. @SpringBootApplication registers a TypeExcludeFilter for component scanning, and in tests Spring Boot adds a filter that excludes classes annotated with @TestConfiguration, as well as test classes themselves. So a @TestConfiguration class is never picked up by scanning, even if it sits in the same package as our application.

The reason is safety. A test configuration might replace the Clock with a fixed one or the payment client with a fake. If scanning picked it up, every test in the module, and in a bad case the application run from the IDE with test classes on the classpath, would get that fake. Because it is excluded, it only applies where a test asks for it.

Java
@TestConfiguration
public class FixedClockConfig {
@Bean Clock testClock() {
return Clock.fixed(Instant.parse("2026-01-01T00:00:00Z"), ZoneOffset.UTC);
}
}
// A plain @Configuration in the same package would be scanned into EVERY test context:
@Configuration
public class FakePaymentsConfig { /* ... */ } // leaks into all @SpringBootTest classes

Notice a catch: the exclusion works because the scan uses the TypeExcludeFilter that @SpringBootApplication sets up. If an application declares its own @ComponentScan without that filter, test configurations can be scanned after all, so keep the default scan or add the filter.

3Nested Classes: Added or Replacing

Where we put the class decides how it is used. A static nested class annotated with @TestConfiguration is detected automatically by @SpringBootTest and used in addition to the main configuration. The test still finds @SpringBootApplication as usual, and the nested beans are added on top.

Java
@SpringBootTest
class InvoiceServiceTest {
@TestConfiguration // added to the application's context
static class Extra {
@Bean Clock testClock() { return Clock.fixed(Instant.parse("2026-01-01T00:00:00Z"), ZoneOffset.UTC); }
}
@Autowired InvoiceService service; // real application bean: found
}
@SpringBootTest
class SurprisingTest {
@Configuration // REPLACES detection of @SpringBootApplication
static class Only {
@Bean Clock testClock() { return Clock.systemUTC(); }
}
@Autowired InvoiceService service; // NoSuchBeanDefinitionException
}
Where the class sits and what happens
ClassFound howEffect on the context
Static nested @TestConfigurationautomaticallyadded to the main configuration
Static nested @Configurationautomaticallyreplaces the main configuration
Top-level @TestConfigurationonly with @Importadded where imported
Top-level @Configuration in a scanned packagecomponent scanadded to every context, a leak
Only the first and third rows are usually what we want.

The second case is the common surprise. A nested @Configuration means here is my configuration, so Spring Boot stops searching for the application class. The context contains only the beans of that nested class, and everything else is missing. If we wanted that, it is fine for a small focused test; usually we wanted @TestConfiguration.

4Top-Level Classes Need @Import

A top-level @TestConfiguration class, one in its own file, is useful when several test classes need the same test beans. Because scanning excludes it and it is not nested, nothing finds it automatically. We include it explicitly with @Import(FixedClockConfig.class) on the test class, or on a shared meta-annotation or base class. This is also good documentation: anyone reading the test sees exactly which test beans are added.

5How the Choice Affects the Context Cache

Spring's TestContext framework caches application contexts between test classes, keyed by everything that defines the context: the configuration classes, active profiles, properties, imported classes, bean overrides and so on. Two test classes with an identical key share one context; a different key means a new context starts, which can take several seconds each.

So every distinct set of test configurations is a new cache key. If ten test classes each have their own nested @TestConfiguration with the same fixed clock, we pay for ten contexts. If they all @Import the same top-level FixedClockConfig, they share one. That is a strong reason to keep reusable test configurations as shared top-level classes.

6Follow-up questions

What happens if a test configuration defines a bean with the same name as the application's bean?

Spring Boot disables bean definition overriding by default, so registering a second bean with the same name fails at startup with a BeanDefinitionOverrideException that names both definitions. If the name differs but the type is the same, startup succeeds but injection by type fails with NoUniqueBeanDefinitionException unless one bean is marked @Primary. The clean ways to replace a bean are a differently named @Primary test bean, or @TestBean and @MockitoBean, which are designed to override.

Can I use @TestConfiguration with slice tests like @WebMvcTest?

Yes. A nested @TestConfiguration is picked up and added to the slice, and a top-level one can be imported. This is a common way to add a bean the slice does not include, such as a security configuration or a fixed clock.

Why not just put test beans in a profile instead?

A profile-specific @Configuration is still found by scanning and only switched on by the profile, so it is easier to activate by mistake and harder to see from the test. @TestConfiguration with @Import keeps the test beans next to the tests that use them and out of the application entirely.

7Conclusion

In this answer, we saw that @TestConfiguration is a @Configuration that Spring Boot's type exclude filter keeps out of component scanning, so it never leaks into the application or other tests. A static nested @TestConfiguration is added automatically on top of the main configuration, a nested @Configuration replaces the main configuration, and a top-level @TestConfiguration must be imported with @Import. Each distinct set of test configurations changes the context cache key, so shared top-level configurations save startup time.

What to say in the interviewAbout 80 seconds aloud

Open with

Both declare beans, but they are found differently. TestConfiguration is a configuration meant only for tests, and Spring Boot's TypeExcludeFilter excludes it from component scanning, so even if it sits in the application's package it never ends up in the app or in unrelated tests.

Then, if they want more

A plain Configuration in a scanned package would be picked up by every test context, which is how fakes leak. For detection, a static nested TestConfiguration inside a test class is found automatically and added on top of the application configuration, so I still get all my real beans plus the test ones.

A nested Configuration is different: SpringBootTest treats it as the whole configuration and stops looking for the SpringBootApplication class, so real beans disappear, which surprises people. A top-level TestConfiguration in its own file is not scanned and not nested, so I have to Import it explicitly.

I usually prefer that for shared test beans, because the context cache key includes the imported configurations. If ten test classes import the same class, they share one context; if each has its own nested copy, I start ten contexts.

And if a test bean has the same name as an application bean, startup fails because overriding is disabled, so I use Primary or TestBean instead.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Spring Boot reference, Testing
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is the difference between @TestConfiguration and @Configuration in Spring Boot...": https://scrivio-interview-library.netlify.app/spring-boot/spring-boot-test/d2204b84d3/