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:

Làm thế nào tạo instance của một class mà không gọi từ khóa new?
Weak References in Java
Hướng dẫn sử dụng Java Annotation
Java Program to Implement Hash Tables Chaining with List Heads
Jackson Exceptions – Problems and Solutions
Apache Commons Collections BidiMap
Xây dựng ứng dụng Client-Server với Socket trong Java
Sao chép các phần tử của một mảng sang mảng khác như thế nào?
Hướng dẫn Java Design Pattern – DAO
Java Program to Check whether Graph is a Bipartite using 2 Color Algorithm
Extra Login Fields with Spring Security
Apache Tiles Integration with Spring MVC
Java Program to Implement Gaussian Elimination Algorithm
Java Program to Find Path Between Two Nodes in a Graph
Enum trong java
Inheritance with Jackson
So sánh HashSet, LinkedHashSet và TreeSet trong Java
New Features in Java 12
Java Program to Permute All Letters of an Input String
Giới thiệu SOAP UI và thực hiện test Web Service
Java Program to Check whether Undirected Graph is Connected using BFS
Concurrent Test Execution in Spring 5
Java Program to Implement Sieve Of Sundaram
Integer Constant Pool trong Java
Java Program to Find Hamiltonian Cycle in an UnWeighted Graph
Introduction to Spring Cloud OpenFeign
Java Program to Check the Connectivity of Graph Using BFS
Hướng dẫn Java Design Pattern – Flyweight
Java Web Services – Jersey JAX-RS – REST và sử dụng REST API testing tools với Postman
New Stream Collectors in Java 9
Java Program to Implement Sparse Array
Remove HTML tags from a file to extract only the TEXT