Authors Top

If you have a few years of experience in the Java ecosystem, and you’d like to share that with the community, have a look at our Contribution Guidelines.

Spring Top – Temp

Get started with Spring 5 and Spring Boot 2, through the reference Learn Spring course:

Lightrun – Third Party Code
announcement - icon

Flakiness in REST requests is a common issue. A request can get a 200 OK in one scenario and a 409 next time. Sometimes a request can even succeed and fail intermittently on the same exact request. In short, working over HTTP can be a bit of a mess without solid tooling.

Also, while it’s easy enough to debug these issues locally when developing the application, we’re talking about production here - we can’t afford the downtime while you’re stepping in and out of code. Uptime is kind of the whole point.

With Lightrun, you can get the same level of access you get with a local debugger or profiler - no downtime required. You can add logs, metrics, and snapshots (think breakpoints, but without stopping the running service), in a safe and read-only manner - without redeploying, restarting, or even stopping the running service. Performance and security are maintained throughout the process.

Learn how to debug a live REST API (built with Spring, of course), using Lightrun, in this 5-minute tutorial:

>> Debugging REST Requests in Spring-Based applications using the Lightrun Platform

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:

public class SpringJUnitConfigIntegrationTest {

    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:

private ApplicationContext applicationContext;

void givenAppContext_WhenInjected_ThenItShouldNotBeNull() {

Finally, here we have the equivalent code of @SpringJUnitConfig(SpringJUnitConfigTest.Config.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:

public class SpringJUnitWebConfigIntegrationTest {

    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:

private WebApplicationContext webAppContext;

void givenWebAppContext_WhenInjected_ThenItShouldNotBeNull() {

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

@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.

Spring bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

Junit footer banner
Comments are closed on this article!