Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Testing Exceptions | Exceptions
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

bookTesting Exceptions

Throwing exceptions also requires unit testing. In JUnit 5, methods for testing such exceptions were added. The logic behind them is simple:

"if it is expected that an exception will be thrown but it is not, then the method is not working correctly, and the test will fail."

Conversely, "if it is expected that no exception will be thrown but the method throws one, then the method is not working correctly, and the test will also fail."

Let's consider these two assertions used to test exception throwing: assertThrows() and assertDoesNotThrow():

`assertThrows()`

As the name suggests, this method will test whether an exception is thrown, expecting that an exception will be thrown.
This is done using a lambda expression. For example, let's test the throwing of a NullPointerException when using the method on a null string:

As you can see, we create a variable with the type String and a value of null, and then test the throwing of an exception when calling the .length() method on this variable. When you call methods on a String with a null value, a NullPointerException is thrown.
In the assertThrows assertion, we specify the class of the exception we expect, which in our case is NullPointerException.class. Then, using a lambda expression, we specify under what condition this exception should be thrown. The syntax is simple:

In other words:

We expect a NullPointerException to be thrown when calling the .length() method on the str object, whose value is null.

It's also worth noting that the assertThrows() assertion returns the exception that was caught and tested. This means we can initialize a variable, exception, using this method and then use it later.
For example:

Best Practices

  1. Test One Exception per Test: Each test method should ideally test for only one specific exception. This makes your tests more maintainable and their intent clearer;
  2. Test Exception Details: Whenever possible, test not only the type of the exception but also its message or other properties. This ensures that the right exception is thrown for the right reason;
  3. Use the Latest Features: If you're using JUnit 5, prefer the assertThrows approach for its flexibility and clarity. It allows not only checking the type of the thrown exception but also enables you to perform further assertions on the exception object.

`assertDoesNotThrow()`

Sometimes, we need to test a method that throws an exception, but in our case, it shouldn't. This is done using the 'assertDoesNotThrow()' assertion. The algorithm here is the same as with the 'assertThrows()' assertion, except that we don't specify the exception we expect to receive ( because we don't expect to receive it ).

The syntax will look like this:

As you can see in this test, we check that the .length() method does not throw an exception when called on a non-null object. After that, we verify that the method correctly performs its function and returns the right result. As you can see, we don't use an exception class in the parameters because we don't expect any exceptions.

It all looks straightforward. In the next chapter, let's see how you handle writing such tests on your own!

1. What is the purpose of `assertThrows()` in JUnit 5?
2. How is a **lambda expression** used in conjunction with `assertThrows()`?
3. Which method can be used to test that no exceptions are thrown by a method?
4. What does `assertThrows()` return in JUnit 5?
5. Why is it recommended to test one exception per test method?
6. In the context of exception testing, why is it important to test exception details like the message?
What is the purpose of `assertThrows()` in JUnit 5?

What is the purpose of assertThrows() in JUnit 5?

Select the correct answer

How is a **lambda expression** used in conjunction with `assertThrows()`?

How is a lambda expression used in conjunction with assertThrows()?

Select the correct answer

Which method can be used to test that no exceptions are thrown by a method?

Which method can be used to test that no exceptions are thrown by a method?

Select the correct answer

What does `assertThrows()` return in JUnit 5?

What does assertThrows() return in JUnit 5?

Select the correct answer

Why is it recommended to test one exception per test method?

Why is it recommended to test one exception per test method?

Select the correct answer

In the context of exception testing, why is it important to test exception details like the message?

In the context of exception testing, why is it important to test exception details like the message?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 6
some-alt