@ContextConfiguration
location attribute does not make sense for Spring Boot integration testing. Is there any other way for reusing application context across multiple test classes annotated with @SpringBootTest
?

- 23,973
- 10
- 81
- 92

- 1,714
- 2
- 19
- 33
5 Answers
Yes. Actually it is default behavior. The link point to Spring Framework docs, which is used by Spring Boot under the hood.
BTW, context is reused by default also when @ContextConfiguration
is used as well.

- 9,785
- 9
- 61
- 73

- 23,973
- 10
- 81
- 92
-
2Perhaps its a good idea to point vicusbass to the docs? https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications – Sean Carroll May 25 '17 at 13:03
-
@SeanCarroll, yes. Wnated to do that, but was distracted and forgot to get back. Answer is updated now. Thanks. – luboskrnac May 25 '17 at 13:07
-
You're right. I had `@DirtiesContext` on the test classes, I misunderstood what it does. I removed it everywhere (there's cleanup in the class setup anyway) and tests are running 3 minutes faster, that's 30% – vicusbass Jun 13 '17 at 11:23
If you land here from Google and have an issue with multiple application contexts being started, also take note of this:
Make sure that when you use @SpringBootTests multiple times that you use the same properties.
E.g. if you have one test using simply @SpringBootTest
and another one using @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
each will spin up its own context!
Easiest would be to have a BaseIntegrationTest
class which you extend in every integration test and put the @SpringBootTest
annotation on that base class, e.g.:
package com.example.demo;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public abstract class BaseIntegrationTest{
}

- 988
- 7
- 10
-
Landed here from google :) .. I guess but in this Abstract class BaseIntegrationTest, we would have to add all the @MockBean so that the context remains the same in all the tests? – user2441441 Jun 06 '22 at 20:16
-
This one was really helpful for me, because I had the opposite problem: I needed tests to run in separate contexts because I wanted to test the loading of environment variables and default values via the application.yml, and either one test or the other would fail if they were run in the same context. But after anottating one of the two test classes with `@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)`, it worked like a charm. Thank you! =^,^= – Kira Resari Feb 17 '23 at 06:57
For those like me landing from Google:
If you have <reuseFork>false</reuseFork>
in your Maven surefire plugin, there is no chance your context can be reused, as you're effectively spawning a new JVM for each test class.
This is well documented in Spring Documentation: https://docs.spring.io/spring/docs/current/spring-framework-reference/testing.html#testcontext-ctx-management-caching

- 4,579
- 1
- 27
- 32
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
The above annotation says the complete context is loaded and same is used across the tests. It means it's loaded once only.
Spring Boot provides a @SpringBootTest annotation which can be used as an alternative to the standard spring-test @ContextConfiguration annotation when you need Spring Boot features. The annotation works by creating the ApplicationContext used in your tests via SpringApplication

- 2,235
- 1
- 12
- 13
-
4That's how my tests are configured. However, I have `@DirtiesContext` annotation and that's what's restarting everything for each test class. `@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)` I'm investigating if I can remove it, but so far I see some problems with the tables – vicusbass Jun 13 '17 at 10:52