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:

Java Program to Implement Caesar Cypher
Java Program to Perform Searching Using Self-Organizing Lists
Use Liquibase to Safely Evolve Your Database Schema
Java CyclicBarrier vs CountDownLatch
Spring Boot - Apache Kafka
Validate email address exists or not by Java Code
Xử lý ngoại lệ đối với trường hợp ghi đè phương thức trong java
Java Program to Implement the Hungarian Algorithm for Bipartite Matching
Java Program to Create the Prufer Code for a Tree
Java Program to Generate Random Numbers Using Multiply with Carry Method
Spring Webflux with Kotlin
Remove the First Element from a List
Java Program to Implement Ford–Fulkerson Algorithm
Inheritance with Jackson
New in Spring Security OAuth2 – Verify Claims
Java Program to Check if a Given Set of Three Points Lie on a Single Line or Not
Java Program to Test Using DFS Whether a Directed Graph is Weakly Connected or Not
Binary Numbers in Java
Các nguyên lý thiết kế hướng đối tượng – SOLID
Show Hibernate/JPA SQL Statements from Spring Boot
Annotation trong Java 8
Java Program to Implement vector
Custom JUnit 4 Test Runners
Immutable Map Implementations in Java
Java Program to Perform the Sorting Using Counting Sort
Java Program to Implement Weight Balanced Tree
Java Program to Implement Flood Fill Algorithm
Spring – Injecting Collections
Guide to java.util.Formatter
Java Program to Check Cycle in a Graph using Graph traversal
OAuth2 for a Spring REST API – Handle the Refresh Token in Angular
Flattening Nested Collections in Java