Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use one spring boot context through all SpringBootTests

I want to be able to cache application context through different classes with tests using junit.

Test classes are declared this way:

@SpringBootTest
@RunWith(SpringRunner.class)
public class SomeIntegrationTest {
}

I saw this question Reuse spring application context across junit test classes but in this case I don't use any xml and I want to start context completely, not just few beans from it, so @SpringBootTest is more suitable than @ContextConfiguration, if I got it right.

like image 933
Ruslan Akhundov Avatar asked Aug 09 '17 12:08

Ruslan Akhundov


People also ask

What does SpringBootTest annotation do?

The @SpringBootTest annotation is useful when we need to bootstrap the entire container. The annotation works by creating the ApplicationContext that will be utilized in our tests. We can use the webEnvironment attribute of @SpringBootTest to configure our runtime environment; we're using WebEnvironment.

What is SpringBootTest WebEnvironment?

public static final SpringBootTest.WebEnvironment RANDOM_PORT. Creates a web application context (reactive or servlet based) and sets a server. port=0 Environment property (which usually triggers listening on a random port). Often used in conjunction with a @LocalServerPort injected field on the test.

Does SpringBootTest start a server?

By default, @SpringBootTest will not start a server. You can use the webEnvironment attribute of @SpringBootTest to further refine how your tests run: MOCK (Default) : Loads a web ApplicationContext and provides a mock web environment. Embedded servers are not started when using this annotation.

Does Spring boot use application context?

ApplicationContext is a corner stone of a Spring Boot application. It represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the beans. The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata.


1 Answers

Ruslan, so your question is on how to reuse the Spring Boot Context for a JUnit Suite, right?

Then, it's almost out-of-the-box provided, you just need to annotate each unit test with the @SpringBootTest annotation.

Also make sure that your main @SpringBootApplication class is loading all the necessary @Configuration classes, this process will be automatically done if the @SpringBootApplication is on the root package above all configuration class and with the inherited @ComponentScan will load up all of them.

From the Spring Boot Testing documentation:

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. The Spring TestContext framework stores application contexts in a static cache. This means that the context is literally stored in a static variable. In other words, if tests execute in separate processes the static cache will be cleared between each test execution, and this will effectively disable the caching mechanism. To benefit from the caching mechanism, all tests must run within the same process or test suite. This can be achieved by executing all tests as a group within an IDE

From the Spring Testing documentation:

By default, once loaded, the configured ApplicationContext is reused for each test. Thus the setup cost is incurred only once per test suite, and subsequent test execution is much faster. In this context, the term test suite means all tests run in the same JVM

Check this urls:

  • http://docs.spring.io/spring/docs/current/spring-framework-reference/html/integration-testing.html#testcontext-ctx-management-caching
  • http://docs.spring.io/spring/docs/current/spring-framework-reference/html/integration-testing.html#testing-ctx-management

Main Takeaways:

  • Annotate each unit test with @SpringBootTest

  • Load all beans and necessary configuration classes in your main @SpringBootApplication class

  • IMPORTANT: Run a JUnit Suite, not a single JUnit test. Execute all tests as a group within your IDE.

like image 97
Andres Cespedes Morales Avatar answered Oct 03 '22 17:10

Andres Cespedes Morales