Master the most popular testing framework for Java, through the Learn JUnit course:
Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.
Get started with mocking and improve your application tests using our Mockito guide:
Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.
Get started with understanding multi-threaded applications with our Java Concurrency guide:
Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:
Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.
But these can also be overused and fall into some common pitfalls.
To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:
Get started with Spring and Spring Boot, through the Learn Spring course:
>> LEARN SPRINGExplore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:
Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.
I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.
You can explore the course here:
Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.
Get started with Spring Data JPA through the guided reference course:
Refactor Java code safely β and automatically β with OpenRewrite.
Refactoring big codebases by hand is slow, risky, and easy to put off. Thatβs where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.
Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions β one for newcomers and one for experienced users. Youβll see how recipes work, how to apply them across projects, and how to modernize code with confidence.
Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.
1. Introduction
In this quick article, weβll take a look at the new @SpringJUnitConfig and @SpringJUnitWebConfig annotations available in Spring and Spring Boot.
These annotations are a composition of JUnit 5 and Spring 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
2.1. Test Configuration
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)
2.2. The loader Attribute
One of the attributes of @ContextConfiguration named loader wasnβt an attribute in @SpringJUnitConfig when it was initially released. In a case where we need to explicitly set a custom loader, we have to revert to the lower-level annotations:
@ExtendWith(SpringExtension.class)
@ContextConfiguration(loader = SpringConfigTest.class)
As of Spring version 6 and Spring Boot version 3.2.0, the loader attribute is available as a parameter in the @SpringJUnitConfig annotation. This provides support for configuring a custom ContextLoader or SmartContextLoader.
For example, letβs explicitly specify annotation-based loading directly in the @SpringJUnitConfig annotation:
@SpringJUnitConfig(classes = TestConfig.class, loader = AnnotationConfigContextLoader.class)
public class SpringJUnitConfigurationUnitTest {
@ParameterizedTest
@ValueSource(strings = { "Dilbert", "Wally" })
void whenSetPeopleWithName_thenListContainsOnePerson(String name, @Autowired List people) {
assertThat(people.stream()
.map(Person::getName)
.filter(name::equals)).hasSize(1);
}
}
Using the loader attribute, we have a simpler test configuration without the need for multiple annotations.
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)
Similarly, Spring version 6 and Spring Boot version 3.2.0 add loader support to @SpringJUnitWebConfig to configure a custom context loader for tests. This gives the annotation the full capabilities of @ContextConfiguration annotation.
4. Conclusion
In this brief tutorial, we showed how to use the introduced @SpringJUnitConfig and @SpringJUnitWebConfig annotations in Spring 5 and above.
