Reactively Writing Unit Tests for Domain Objects

Learn how to write unit tests and test domain objects.

We'll cover the following

The simplest, fastest, and easiest way to test anything is at the unit level. A unit is most commonly expressed in Java as a single class. In essence, a unit test involves verifying a single class acts properly by removing any external collaborators.

Why is this the simplest way to test?

This is because there’s little machinery needed to put this together. In fact, we can do this without any machinery. However, it’s handy to use something like JUnit 5 to at least gather the results. In addition to that, there are powerful assertion libraries to help verify results.

Note: Do we need JUnit 4 or JUnit 5?

That’s easy. As great as JUnit 4 is, it’s effectively feature-complete. All new development by the JUnit team is being poured into JUnit 5. When starting anything new, we’d pick JUnit 5.

If we have an existing system with lots of test cases written using JUnit 4, the transition can cost us a bit. However, it’s not that hard to migrate.

What does Spring Boot offer, if no assistance is required?

Spring Boot upholds the importance of testing so much, that a suite of test libraries comes preconfigured. We simply have to add spring-boot-starter-test to our build file and the following test libraries are automatically added:

Get hands-on with 1200+ tech skills courses.