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:

Creating Docker Images with Spring Boot
Intro to Inversion of Control and Dependency Injection with Spring
Java Program to Generate All Possible Combinations of a Given List of Numbers
A Guide to WatchService in Java NIO2
Java Program to Implement Find all Back Edges in a Graph
Default Password Encoder in Spring Security 5
Java Program to Solve any Linear Equations
Java Program to implement Dynamic Array
Guide to the Synchronized Keyword in Java
Java Program to Implement Hash Tables Chaining with Binary Trees
Introduction to Spring Cloud Stream
Java Program to Generate a Random UnDirected Graph for a Given Number of Edges
Java Program to find the peak element of an array using Binary Search approach
Map Serialization and Deserialization with Jackson
Introduction to Spring Boot CLI
Spring MVC Custom Validation
Java – Try with Resources
Chuyển đổi từ HashMap sang ArrayList
Runnable vs. Callable in Java
Setting Up Swagger 2 with a Spring REST API
Object Type Casting in Java
Java Program to Find the Peak Element of an Array O(n) time (Naive Method)
Java Program to Check whether Undirected Graph is Connected using DFS
Spring Boot - Runners
Java Program to Implement Stein GCD Algorithm
Deploy a Spring Boot WAR into a Tomcat Server
Mix plain text and HTML content in a mail
Java Program to Describe the Representation of Graph using Incidence Matrix
Kiểu dữ liệu Ngày Giờ (Date Time) trong java
Registration – Activate a New Account by Email
Java Program to Compute DFT Coefficients Directly
Generating Random Dates in Java