Conteúdo do Curso
Java JUnit Library. Types of Testing
Java JUnit Library. Types of Testing
Testing 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
-
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;
-
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;
-
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!
Obrigado pelo seu feedback!