The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5

1. Introduction

In this quick article, we’ll take a look at the new @SpringJUnitConfig and @SpringJUnitWebConfig annotations available in Spring 5.

These annotations are a composition of JUnit 5 and Spring 5 annotations that make test creation easier and faster.

2. @SpringJUnitConfig

@SpringJUnitConfig combines these 2 annotations:

  • @ExtendWith(SpringExtension.class) from JUnit 5 to run the test with the SpringExtension class and
  • @ContextConfiguration from Spring Testing to load the Spring context

Let’s create a test and use this annotation in practice:

@SpringJUnitConfig(SpringJUnitConfigIntegrationTest.Config.class)
public class SpringJUnitConfigIntegrationTest {

    @Configuration
    static class Config {}
}

Notice that, in contrast to the @ContextConfiguration, configuration classes are declared using the value attribute. However, resource locations should be specified with the locations attribute.

We can now verify that the Spring context was really loaded:

@Autowired
private ApplicationContext applicationContext;

@Test
void givenAppContext_WhenInjected_ThenItShouldNotBeNull() {
    assertNotNull(applicationContext);
}

Finally, here we have the equivalent code of @SpringJUnitConfig(SpringJUnitConfigTest.Config.class):

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = SpringJUnitConfigTest.Config.class)

3. @SpringJUnitWebConfig

@SpringJUnitWebConfig combines the same annotations of @SpringJUnitConfig plus the @WebAppConfiguration from Spring testing – to load the WebApplicationContext.

Let’s see how this annotation works:

@SpringJUnitWebConfig(SpringJUnitWebConfigIntegrationTest.Config.class)
public class SpringJUnitWebConfigIntegrationTest {

    @Configuration
    static class Config {
    }
}

Like @SpringJUnitConfig, the configuration classes go in the value attribute and any resources are specified using the locations attribute.

Also, the value attribute of @WebAppConfiguration should now be specified using the resourcePath attribute. By default, this attribute is set to “src/main/webapp”.

Let’s now verify that the WebApplicationContext was really loaded:

@Autowired
private WebApplicationContext webAppContext;

@Test
void givenWebAppContext_WhenInjected_ThenItShouldNotBeNull() {
    assertNotNull(webAppContext);
}

Again, here we have the equivalent code without using @SpringJUnitWebConfig:

@ExtendWith(SpringExtension.class)
@WebAppConfiguration
@ContextConfiguration(classes = SpringJUnitWebConfigIntegrationTest.Config.class)

4. Conclusion

In this brief tutorial, we showed how to use the newly introduced @SpringJUnitConfig and @SpringJUnitWebConfig annotations in Spring 5.

The full source code for the examples is available over on GitHub.

Related posts:

Documenting a Spring REST API Using OpenAPI 3.0
Encode/Decode to/from Base64
Java Program to Find Hamiltonian Cycle in an UnWeighted Graph
Java Program to Implement Hash Tables with Quadratic Probing
Java Program to Implement Park-Miller Random Number Generation Algorithm
Java Program to Implement Tarjan Algorithm
Spring Boot - CORS Support
Java Program to Find Strongly Connected Components in Graphs
Tính trừu tượng (Abstraction) trong Java
Guide to Selenium with JUnit / TestNG
Java Program to Print only Odd Numbered Levels of a Tree
Kiểu dữ liệu Ngày Giờ (Date Time) trong java
Phương thức forEach() trong java 8
So sánh ArrayList và Vector trong Java
Introduction to Java 8 Streams
Java Program to Show the Duality Transformation of Line and Point
Control Structures in Java
Java Program to Implement Pagoda
Java Program to Implement the Alexander Bogomolny’s UnOrdered Permutation Algorithm for Elements Fro...
A Guide to the Java ExecutorService
Creating a Generic Array in Java
Servlet 3 Async Support with Spring MVC and Spring Security
Guide to @ConfigurationProperties in Spring Boot
Java Program to Implement Suffix Tree
Java Program to Find the Mode in a Data Set
Hướng dẫn Java Design Pattern – Dependency Injection
HTTP Authentification and CGI/Servlet
Summing Numbers with Java Streams
Java Web Services – Jersey JAX-RS – REST và sử dụng REST API testing tools với Postman
Java Program to Find Shortest Path Between All Vertices Using Floyd-Warshall’s Algorithm
Java Program to Find Nearest Neighbor Using Linear Search
Arrays.asList vs new ArrayList(Arrays.asList())