Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
How to Test `void` Methods | Unit Tests
Java JUnit Library. Types of Testing
course content

Course Content

Java JUnit Library. Types of Testing

Java JUnit Library. Types of Testing

1. Testing in Development
2. Unit Tests
3. Exceptions

bookHow to Test `void` Methods

The algorithm for writing unit tests is clear to us. First, we specify the input, then the expected result, and finally, the actual result returned by the method under test. But the question arises of how to test methods that don't return anything?

Testing void methods can be a challenging process since we cannot test the return value. However, void methods often change the state of an object, so we can test their functionality in that way.

Counter

Let's write a void method that we will work with; it will be a simple method that increments a value ( yes, Java already has such a method, but I think this method will be an excellent example for testing ):

java

Counter

copy
1234567891011
public class Counter { private int count = 0; public void increment() { count++; } public int getCount() { return count; } }

Above, you can see the Counter class, which contains an increment method and stores a variable called count. For example, if we need to count something, we can simply call this method using the Counter.increment() construct, and the private variable count will increase.

Creating Tests

First, we need to create an instance of this class and then use the void method on this instance.

Next, we simply check if this element is not equal to zero, and if it's not, it means the method worked successfully, and the variable was incremented. This is what the first unit test will look like:

As you can see, we've followed the algorithm, and then we check the expected state of the counter object.

After one increment, the object changed its value to +1, so it should be equal to one, and that's exactly what we are checking.

The only thing left for us to verify is when we increment multiple times. For instance, let's write a unit test where we increment the count variable several times and check the accuracy of the result:

Here, we also create a new object and call the increment() method three times. Then, we compare the result with 3, thus testing whether the method works correctly.

Test Optimization

These tests can also be optimized using the @BeforeEach annotation and creating a Counter object at the beginning of testing.

You can do it like this:

This way, we can eliminate the creation of the counter object from each unit test, as it is now automatically created. Here is the final code for unit testing the void increment() method:

In the next chapter, we will discuss the different types of assertions and how to use each of them.

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 5
We're sorry to hear that something went wrong. What happened?
some-alt