Serengeti logo BLACK white bg w slogan
Menu

Writing Unit Tests in Java

Lovro Cvitaš, Software Developer
17.10.2022.

What is Unit Testing?

Unit testing is a type of software testing where individual software units and components are tested to validate whether each code unit is performing as intended.

While in most software development companies Quality Assurance Engineers write integration tests, regression tests, system tests, user acceptance tests, etc. during the software testing phase of the Software Development Life Cycle (SDLC), unit tests are written by the software during the software development stage.

Introduction

In the Java ecosystem, JUnit is a popular unit-testing framework. Numerous brand-new features based on Java 8 were added to JUnit 5.

The next generation of JUnit is JUnit 5. The objective is to establish a current foundation for JVM developer-side testing. This includes supporting a wide range of testing methods and concentrating on Java versions earlier than 8.

Unlike previous versions of JUnit, JUnit 5 is composed of several different modules from three different sub-projects.

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

The following code defines a minimal test class with one minimal test method.

unit testing

This is an example for developing a JUnit 5 test for another class.

Let's assume that we have the following class we want to test:

unit testing class

A test class for the class above should look like this:

picture7

Basic Annotations

@BeforeAll and @BeforeEach

Below is an example of simple code to be executed before the main test cases:

picture8

Assertions and Assumptions

In unit tests, assertions are collections of utility methods used to back up asserting conditions. In JUnit 4, JUnit 5 and the assertions class, these utility methods can be accessed. It is always recommended to import the relevant class statically to improve the test's and assertions' readability. Without using the representing class as a prefix, in this way we are able to refer directly to the assertion method itself. You can test your code under test with the help of JUnit 5's multiple assert statements. Checking for true, false, or equality is made possible by straightforward assert statements like the one below. All of them are org-specific static methods.JUnit.Jupiter.API.Assertions.*package.

Assert statementExample
assertEqualsassertEquals(9, calculator.multiply(3, 3),"fail message");
assertTrueassertTrue('a' < 'b', () → "fail message");
assertFalseassertFalse('a' > 'b', () → "fail message");
assertNotNullassertNotNull(object, "fail message");
assertNullassertNull(object, "fail message");


picture9

Assumptions

Assumptions are collections of utility methods used to run tests only if certain conditions are met. They are typically used when it does not make sense to continue the test method execution. One example is when test depends on something that does not exist in the current runtime environment.

We can declare an assumption with assumeTrue(), assumeFalse() and assumingThat˙´()

picture10

Conclusion

In this article, only a small part of JUnit 5 has been explored. JUnit 5 makes it easy to write effective and automated software tests. There are many examples and best practices that can be found on the Internet.

Unit tests have proven that they can improve software quality, but most software engineers see them as a waste of time.

In my opinion, software developers should write unit tests because they can save them time later when errors occur.

Let's do business

The project was co-financed by the European Union from the European Regional Development Fund. The content of the site is the sole responsibility of Serengeti ltd.
cross