Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Testing Exceptions | Exceptions
course content

Contenido del Curso

Java JUnit Library. Types of Testing

Testing ExceptionsTesting 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?

Selecciona la respuesta correcta

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

Selecciona la respuesta correcta

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

Selecciona la respuesta correcta

What does assertThrows() return in JUnit 5?

Selecciona la respuesta correcta

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

Selecciona la respuesta correcta

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

Selecciona la respuesta correcta

¿Todo estuvo claro?

Sección 3. Capítulo 6
course content

Contenido del Curso

Java JUnit Library. Types of Testing

Testing ExceptionsTesting 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?

Selecciona la respuesta correcta

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

Selecciona la respuesta correcta

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

Selecciona la respuesta correcta

What does assertThrows() return in JUnit 5?

Selecciona la respuesta correcta

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

Selecciona la respuesta correcta

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

Selecciona la respuesta correcta

¿Todo estuvo claro?

Sección 3. Capítulo 6
some-alt