Search code examples
javaspringspring-bootintegration-testing

Spring Boot Integration Tests - How to run multiple test classes with a single context?


I have a Spring Boot project where I have a couple of api end points defined. I am currently writing integration tests for each of these apis. I annotate my test classes as follows.

@RunWith(SpringRunner.class)
@SpringBootTest
...

Now, for each of my test class, a new context is set up for executing them and it takes time. I prefer to keep the test cases for each of my api end points in separate classes for organizing them logically, but I don't want my test execution time to shoot up every time I add a new controller class and a corresponding test class. What am I doing wrong here?


Solution

  • As long as your tests use identical contexts (same beans mocked, same configuration) spring will, by default, reuse a suitable context before it creates a new one.

    An exception is when you annotate a test with @DirtiesContext This tells the Runner to invalidate the used context.