Override Spring Boot Configuration Properties For Tests

As a general best practice, we should externalize configuration values for our applications. This allows overriding them per stage and e.g. connect to a PayPal test instance for our development stage. Apart from overriding these properties for different stages, we can use the same technique when testing our applications to e.g. connect to a mocked

Read More

Fix No Qualifying Spring Bean Error For Spring Boot Tests

When working for the first time with Spring, you might encounter several no qualifying bean exceptions when you try to start your application. The stack trace is quite long for such error scenarios and it might become frustrating as your application doesn’t start. The same is true whenever your test works with a Spring Test

Read More

Test Your Spring Boot JPA Persistence Layer With @DataJpaTest

Similar to testing our web layer in isolation with @WebMvcTest, Spring Boot provides a convenient way to test our Spring Boot JPA persistence layer: @DataJpaTest. While the default configuration expects an embedded database, this article demonstrates how to test any Spring Data JPA repository in isolation with Testcontainers and Flyway. The sample application uses Spring

Read More

Test Your Spring MVC Controller with the WebTestClient and MockMvc

Starting with Spring Framework version 5.3 (part of Spring Boot since version 2.4.0) you can perform requests with the WebTestClient against MockMvc. This allows unifying the way you invoke your controller endpoints during tests. For both test scenarios (real HTTP communication or mocked Servlet environment), you now have the possibility to use the WebTestClient. WebTestClient

Read More

@Mock vs. @MockBean When Testing Spring Boot Applications

When you start testing your Spring Boot application sooner or later you’ll stumble over @Mock and @MockBean. Both annotations create mock objects but with a slightly different purpose. This might be confusing in the beginning. With this blog post, I’ll resolve this confusion and explain the difference between @Mock and @MockBean when it comes to

Read More

Guide to Testing Spring Boot Applications With MockMvc

Did you ever found yourself saying: I usually ignore testing my Spring Web MVC controller endpoints because the security setup is tricky. That belongs to the past. With MockMvc, Spring provides an excellent tool for testing Spring Boot applications. This guide provides you with recipes to verify your @Controller and @RestController endpoints and other relevant

Read More

Spring Boot Test Slices: Overview and Usage

Spring Boot offers great support to test different slices (web, database, etc.) of your application. This allows you to write tests for specific parts of your application in isolation without bootstrapping the whole Spring Context. Technically this is achieved by creating a Spring Context with only a subset of beans by applying only specific auto-configurations.

Read More

Testing Spring Boot Applications with Kotlin and Testcontainers

Recently I introduced Testcontainers for a Kotlin based Spring Boot application. I jumped right into the setup and immediately ran into an issue. Even though the interoperability of Java libraries and Kotlin is excellent, there is one thing you need to know when using Testcontainers with Kotlin. Continue reading to understand what it takes to

Read More

Improve build times with Context Caching from Spring Test

I’ve recently invested one day to improve the integration test setup for a larger project. The result is amazing: the build time (running mvn clean verify) went down from 25 minutes to 5 minutes by solely focussing on the test setup. All of this was possible by making the most of the Spring Test Context

Read More

Write Integration Tests For Your Spring WebSocket Endpoints

WebSockets allow establishing a full-duplex, two-way communication between the client and the server. With Spring WebSocket you can bootstrap your WebSocket application with ease. While there is great test support available for verifying Spring MVC endpoints (e.g. @RestController) using MockMvc, the support for testing WebSockets is rather limited. Continue reading to get an introduction to

Read More