How to start with unit testing in Java: A complete introduction to JUnit 5

How to start with unit testing in Java: A complete introduction to JUnit 5

How to start with unit testing in Java: A quick introduction to JUnit 5 ... Unit testing is a level of software testing, when we test individual software's ...

Originally published by YURI MEDNIKOV at 

In this post, I would like to focus on unit testing in Java with JUnit5 library. It introduced new features and approaches to testing, compare to older JUnit 4 version, that are worth to check. We would overview what is unit test and why to test; how to install JUnit 5 in your project; what is a basic test structure; how to use Asseritions API and how to combine multiple tests in a test suite.

What is unit testing?

Unit testing is a level of software testing, when we test individual software's components in isolation. For example, we have UserService. It may have various connected dependencies, like UserDAO to connect to a datasource, or EmailProvider to send confirmation emails. But for unit testing, we isolate UserService and may mock connected dependencies (how to do mocking, we would see in the next chapter).

The unit testing offers us a number of benefits, to name few:

  • It increases our confidence, when we change code. If unit tests are good written and if they are run every time any code is changed, we can notice any failures, when we introduce new features
  • It serves as documentation. Certanly, documenting your code includes several instruments, and unit testing is one of them - it describes an expected behaviour of your code to other developers.
  • It makes your code more reusable, as for good unit tests, code components should be modular.

These advantages are just few of numerous, that are provided us by unit testing. Now, when we defined what is unit testing and why do we use it, we are ready to move to JUnit5.

Install JUnit5

Build tools support

For a native support of JUnit5 you should have a version of Gradle 4.6+ or Maven 2.22.0+.

For Maven you need to add to your Pom.xml:


For Gradle add to build.gradle:

testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '$version'

You could find the latest version of JUnit5 in the official repository. By the way, if you would use JUnit 5 with Vert.x framework, there is Vert.x JUnit5 extension.

IDE support

Intelij IDEA supports JUnit5 natively since 2016.2, and Eclipse since 4.7.1a.

An anatomy of unit test

Basic structure

Consider this example: we have a program, that performs a linear search of a number in an array of integers. Here is a main class, that we place in the src/main/java/ folder:

class LinearSearcher(){
private int[] data;

LinearSearcher(int[] arr){ = arr;

int getPositionOfNumber(int value){
int n = data.length;
for(int p = 0; i < n; i++)
if(data[p] == value)
return p;
return -1;

And then add this second code to the src/test/java folder:

class LinearSearcherTest{

private static LinearSearcher searcher;

static void setup(){
   int[] array = {2, 3, 4, 10, 40};
   searcher = new LinearSearcher(array);

//Actual test methods
void getPosition(){
   int result = searcher.getPositionOfNumber(10);

void noSuchNumber(){
   int result = searcher.getPositionOfNumber(55);
   Assertions.assertEquals(-1, result);

static void finish(){
   System.out.println("Tests are finished!");


Let check what we did in this code. We introduced a new class, LinearSearcher that has one method - getPostionOfNumber that returns the position of value in the array, or returns -1, if value is not presented in the array.

In second class, LinearSearcherTest we actually do unit testing. We expect two scenarios: when we have a number in the array (in our case 10), we expect to receive its position (3). If no such number is presented (for example 55), our searcher should return -1. Now, you can run this code and check results.

Before methods

You could note two methods annotated respectively with @BeforeAll and @AfterAll. What do they do? First method corresponds to Before methods. There are two of them:

  • @BeforeAll - the static method that will be executed once before all@Test method in the current class.
  • @BeforeEach - the method that will be executed before each @Test method in the current class.

These methods are handy to setup unit test environment (for example, to create instances).

After methods

As there are before methods, there are After methods. There is also a couple of them:

  • @AfterAll - the static method will be executed once after all @Test methods in the current class.
  • @AfterEach - the method that will be executed after each @Test method in the current class.
Using standard Assertions API

Assertions API is a collection of utility methods that support asserting conditions in tests. There are numerous available methods, however we would focus on most important of them.

Assert not null

When we need to assert, that actual object is not null, we can use this method:

assertNotNull(Object obj);

If object is not null, the method passes, if not - fails.

Assert Equals

This group includes many methods, so I would not provide your all overloaded versions, but would focus a general signature:

assertEquals(expected_value, actual_value, optional_message);

These methods have two required arguments and one optional argument:

  • expected_value = the result, we want to receive
  • actual_value = the tested value
  • optional_ message = String message, that would be displayed to STDOUT if method is failed.

Values can be of primitive types: int, double, float, long, short, boolean, char, byte, as well Strings and Objects. To this group, we can add these test methods:

  • assertArrayEquals - check that expected and actual arrays are equal. Arrays are of primitive types
  • AssertFalse and AssertTrue - check that supplied boolean condition is false or true respectively
  • assertIterableEquals - same as assertArrayEquals, but for Iterables(e.g. List, Set etc)

As I mentioned, there are many overloaded methods in this section, so it worth to explore official documentation for concrete signatures.

Assert throws

This is an innovation of JUnit5. Consider, that you have a method that throws an exception:

Car findCarById(String id) throws FailedProviderException;

This method retrieves an individual Car from an underlaying database by its ID, and throws FailedProviderException when there is a problem with database. In other words, we wrap in an interface possible data source exceptions (like SQLException or respected for NoSQL databases) and achieve its independence from the implementation.

How do we test that exception is thrown? Before, in JUnit4 we used annotations:

@Test(expected = FailedProviderException.class)
void exceptionThrownTest() throws Exception{
Car result = repository.findCarById("non-existing-id");

Btw, same idea is used in TestNG. In JUnit5 was introduced assertThrowsmethod. Take a look, how we would deal with same situation:

void exceptionThrownTest(){
Assertions.assertThrows(FailedProviderException.class, ()->{
Car result = repository.findCarById("non-existing-id");

This method signature has two components:

  1. Expected exception to be thrown
  2. Lambda expression of Executable, that contains a code snippet, that potentially throws the exception.

Again, as we aforesaid methods of assertEquals's group, we can provide an optional message of String as third argument.

Assert timeout

When we need to assert, that test is finished in a defined timeout, we can use this method:

assertTimeout(Duration timeout, Executable executable)

The idea is same as with assertThrows method, but there we specify timeout. Second argument is a same Executable lambda expression. Third optional component is a String message. Let consider an example:

void in3secondsTest(){
Assertions.assertTimeout(Duration.ofSeconds(3), ()->{
//some code

Please note, that this method uses Duration API to specify timeframe. It has several handy methods, like ofSeconds(), ofMills() etc. If you are not familiar with it, don't be shy to check this tutorial.


Finally, what if we need to fail test? Just use method. Again, there are several of them:

  • fail (String message) = Fails a test with the given failure message.
  • fail (String message, Throwable cause) = Fails a test with the given failure message as well as the underlying cause.
  • fail (Throwable cause) = Fails a test with the given underlying cause.
Creating test suites

If you have several unit tests and you want to execute them in one load, you can create a test suite.

This approach allows you to run tests spread into multiple test classes and different packages.

Suppose, that we have tests TestA, TestB, TestC, that divided into three packages: net.mednikov.teststutorial.groupA, net.mednikov.teststutorial.groupA, net.mednikov.teststutorial.groupC respectively. We can write the test suite to combine them:

@SelectPackages({net.mednikov.teststutorial.groupA, net.mednikov.teststutorial.groupB, net.mednikov.teststutorial.groupC})
public class TestSuite(){}

Now, you can run this method as one test suite.

  • Sergio Martin. Take Unit Testing to the Next Level With JUnit 5 (2018). DZone, read here
  • Petri Kainulainen. Writing Assertions with JUnit5 Assertion API (2018), read here
  • J Steven Perry. The JUnit5 Jupiter API (2017) IBM Developer, read here

In this post, we learned what is unit test and why to test; how to install JUnit 5 in your project; what is a basic test structure; how to use Asseritions API and how to combine multiple tests from different packages in a test suite. But, of course, JUnit 5 is a huge topic, and this post is just a top of an iceberg. Some frameworks, like Vert.x, offer special JUnit 5 extensions, like vertx-junit5. Good luck with JUnit5 ! :)

Originally published by YURI MEDNIKOV at 


Thanks for reading :heart: If you liked this post, share it with all of your programming buddies! Follow me on Facebook | Twitter

☞ Java Programming Masterclass for Software Developers

☞ JSP, Servlets and JDBC for Beginners: Build a Database App

☞ Java 8 New Features In Simple Way

☞ Java In-Depth: Become a Complete Java Engineer!

☞ Java for Absolute Beginners

☞ Java Programming for Complete Beginners - Learn in 250 Steps

☞ Learn Java Programming Crash Course

☞ Apache Spark for Java Developers

Mobile App Development Company India | Ecommerce Web Development Company India

Mobile App Development Company India | Ecommerce Web Development Company India

Best Mobile App Development Company India, WebClues Global is one of the leading web and mobile app development company. Our team offers complete IT solutions including Cross-Platform App Development, CMS & E-Commerce, and UI/UX Design.

We are custom eCommerce Development Company working with all types of industry verticals and providing them end-to-end solutions for their eCommerce store development.

Know more about Top E-Commerce Web Development Company

.NET or Java for Web Development

.NET or Java for Web Development

Developers are often in a dilemma when trying to choose between technologies for their projects. Comparing two or more is quite daunting and confusing at times. In this article, we will focus on comparing .NET and Java.

Thinking of adopting one of these technologies for your next web development project? Read on for a basic overview.

Developers are often in a dilemma when trying to choose between technologies for their projects. Comparing two or more is quite daunting and confusing at times. In this article, we will focus on comparing .NET and Java.

The two are quite different in structure but are predisposed to comparison because of their demand when there is a call for complex and large-scale applications.

While .NET is a framework that can use several languages, Java, on the other hand, is a programming language itself.

A debate is often sparked during comparison among business owners and developers when it comes to choosing between the two. In fact, it is common for people to switch between the two periodically.

.NET, under the auspices of its framework, encompasses C#, which is basically Java, and here lies the challenge in contrasting between the better of the two. This article describes how the two are different while showing their similarity.

What Is Common to Both Java and .NET?

They Are Developed for Heavy Applications

Heavy execution, complex architecture, systems with high loads, and applications with big data specifications are some of the things that can be executed by both Java and C# in the .NET framework. This accords them the high adoption rates in technologies in the enterprise level which proves their scalability and reliability.

They Are Multipurpose

Ranging from subtle to overt, both Java and the .NET framework can be run on desktops, mobile applications, and servers. They are both widely used in the enterprise market. Both Java and C# are 'write one run anywhere' type languages.

They Are Similar in Their Syntax

C# in the .NET framework is derived from the Java language. This proves that the syntaxes of Java and C# are the same except for some basic modifications that were made. A basic syntax is common between the two since Java uses syntax from C++ and C# uses a variety of C languages and style including C, C++, and Java.

Both Are Object-Oriented

A common standard, object-oriented programming, is a basic principle of software development. Having their structure in modules, both Java and .NET are flexible and allow for code reuse while having good troubleshooting expansions.

Garbage Collection

Common to low-level languages, recalling is necessary if you have to free up space during memory administration which makes it a repetitive process. However, in .NET and Java there are no memory spills since objects that are not in use are evacuated.

Single Inheritance

Both C# and Java reinforce single inheritance. This means that a single path is always available from a base class to an inferred class.


Whenever methods are unique in an interface for a dynamic class, it defines an interface. A dynamic method on the other and is one that does not encompass its usage point of interest. Any interface characterization, in terms of the property, has code overseeing it, which is provided for actualization based on a specified class for its actualization.

Some of the Differences

Both Are Portable but One Exceeds the Other

Migrating between Java platforms is easy compared to the .NET framework, which is time-consuming and difficult.

Both Have Fundamental Data Types but One Exceeds the Other

.NET incorporates more fundamental data types than Java. It also allows for more extension of these value types.

They Interact Well With Different Languages

Both Java and .NET have their own languages, which they can easily integrate and work with. Examples for Java include Clojure and Groovy while .NET includes visual basic, F# and C#, among others.

Choosing .NET Core or Java for your web development project is really dependent on the type of project, the resources you have, and many other factors.

Happy web developing!