Welcome to another edition of Developreneur. Today we are going to be talking about AssertJ packages. AssertJ is a fluent assertion library in Java for unit testing. Therefore, instead of using the traditional JUnit Assert class, we will discuss this new library, AssertJ. Today we’ll be looking at unit testing with Assert J. We’ll briefly talk about the differences between assertions and why they’re essential within unit testing. And then, we’re going to go into a full HandsOn presentation using assert j methods and chaining within our unit testing.

What is Unit Testing?

Let’s begin by talking about unit testing. Unit testing is software testing, where we test the individual units of our source code. Here we focus on testing particular methods or units of work, not the general application. We’re not doing regression or integration testing on the different integration points or other applications. We want to focus on a specific unit of work, potentially a method in our code.

Why is unit testing necessary?

First, it helps us to identify problems early in the development cycle. Then it lets us quickly catch bugs or issues with our code changes at development time. So as we’re writing the code, we can test the code and say, oops, our change broke something, or everything passed, and we can then move on. Finding and fixing bugs at this level is very cost-effective because we can fix them now. Whereas if we waited till we did regression testing or manual testing of the application once it’s been packaged and shipped. It will be harder for us to identify what that bug is or where that problem came from in the code.

Finding and fixing bugs at this level is very cost-effective because we can fix them now.

The importance of Unit testing

With Unit testing, we can quickly catch most of the functionality bugs within our application during development. It also makes debugging the process easier because if we see the bug in our unit test, we know specifically in our code where it broke. We know where to fix the problem. Again, if we wait till the end when we’re trying to package and ship the product, we don’t necessarily know where the bug exists. So it will take more time and effort to go back in and dig into the code and find the problem. The other nice thing about unit testing is we can reuse our code to migrate our specific test from one application to another. So if we have similar code in one application, we could copy that unit test, put it into the other code base, make a couple of tweaks, and be done.

Why is unit testing so effective?

Unit testing is the first line of testing that developers can do while writing their source code. Now, this leads to assertions. So what are assertions? The Oxford Dictionary defines assertion as a confident and forceful statement of fact or belief. Which translates into what we’re looking at when we talk about testing. Junit or unit testing defines an assertion as a collection of utility methods that support asserting test conditions. It is easy to take a test scenario or some test data and test something. We want to make sure that things work.

An assertion is a confident and forceful statement of fact or belief.

So what is AssertJ?

AssertJ is a library or a third-party library that you can include in your project that is rich and easy to use. It is extensible and open source so that you can extend the libraries. It is also community-driven, so there are no licenses or costs to using this library. AssertJ provides a rich set of assert classes and methods that provide excellent error messages. It improves test code readability and is easy to use within your favorite IDE.

Why should I use AssertJ instead of plain Assert classes?

The whole reason I’m even talking about AssertJ today is that once I found and started using this, it made writing those unit tests, asserts, and use cases easier to read and follow. Instead of having to create a whole bunch of independent asserts with the traditional assert libraries that come with Junit or some of the other unit libraries. It made a difference in the way I write Unit tests.

Hand on with AssertJ

In the video, we also provide a hands-on demo of the different APIs between JUnit and AssertJ. Including a sample unit test highlighting some of the functionalities that come with AssertJ.

Finally, this series comes from our mentoring/mastermind classes.  These classes are virtual meetings that focus on how to improve our technical skills and build our businesses.  After all, the goals of each member vary.  However, this diversity makes for great discussions and a ton of educational value every time we meet.  We hope you enjoy viewing this series as much as we enjoy creating it.  As always, this may not be all new to you, but we hope it helps you be a better developer.

Other Classes, You Might Consider:

Leave a Reply