Maven Setup For Testing Java Applications

Starting with a new programming language is always exciting. However, it can be overwhelming as we have to get comfortable with the language, the tools, conventions, and the general development workflow. This holds true for both developing and testing our applications. When testing Java applications with Maven, there are several concepts and conventions to understand:

Read More

Guide to @SpringBootTest for Spring Boot Integration Tests

With this guide, we’ll investigate the powerful @SpringBootTest annotation for writing integration tests. If you’re new to testing the Spring Boot applications, start with the testing overview and get a basic understanding of the testing swiss-army knife Spring Boot Starter Test. The Spring ecosystem seamlessly integrates various technologies and tools, but it also provides excellent

Read More

Creating Deep Stubs With Mockito to Chain Method Stubbing

Yet another blog post about a Mockito feature that we should rarely use: Mockito deep stubs. With this article, we’ll explore how deep stubbing can reduce the boilerplate stub setup for our tests when chaining fluent APIs of a mocked class. In general, “Every time a mock returns a mock, a fairy dies” should be

Read More

Spring Boot Unit and Integration Testing Overview

With this blog, you’ll get an overview of how unit and integration testing works with Spring Boot. On top of this, you’ll learn what Spring features and libraries to focus on first. This article acts as an aggregator and at several places, you’ll find links to other articles and guides that explain the concepts in

Read More

Initialization Strategies With Testcontainers For Integration Tests

Testcontainers offers several initialization strategies for our Docker containers when writing integration tests. Depending on the Docker image we use for our tests, we might have to perform additional setup steps. This includes adjusting the container configuration or populating data. With Testcontainers, we can tweak the container configuration either during runtime (executing commands inside the

Read More

Record Spring Events When Testing Spring Boot Applications

One of the core features of Spring is the event publishing functionality. We can use events to decouple parts of our application and implement a publish-subscribe pattern. One part of our application can publish an event that multiple listeners (even asynchronously) react to. As part of Spring Framework 5.3.3 (Spring Boot 2.4.2), we can now

Read More

Testing Spring Boot Applications With REST Assured

REST Assured is a Java DSL (Domain Specific Langauge) that aims to simplify testing REST APIs. It follows a BDD (Behavior Driven Development) approach and is influenced by testing APIs with dynamic languages like Groovy. We can use REST Assured to test the REST API of any Java project as it is framework independent. However,

Read More

Mock Java Constructors With Mockito | Configuration and Examples

Starting with Mockito version 3.5.0, we can now mock Java constructors with Mockito. This allows us to return a mock from every object construction for testing purposes. Similar to mocking static method calls with Mockito, we can define the scope of when to return a mock from a Java constructor for a particular Java class.

Read More

MongoDB Testcontainers Setup for @DataMongoTest

MongoDB is one of the NoSQL databases that Spring Boot offers great testing support for. Like all other test slice annotations from Spring Boot, when using @DataMongoTest, we’ll get a Spring Test Context with just enough beans to test any MongoDB-related code. By default, Spring Boot tries to start and use an embedded MongoDB instance.

Read More

Use GreenMail For Spring Mail (JavaMailSender) JUnit 5 Integration Tests

Sending emails is a common responsibility of enterprise applications. With the Spring Boot Starter Mail dependency, this becomes a trivial task. But how can we write integration tests to verify our functionality without sending actual emails to a user? With this blog post, we’ll introduce GreenMail to write integration tests with JUnit 5 for sending

Read More