Package org.djutils.test
Class UnitTest
java.lang.Object
org.djutils.test.UnitTest
UnitTest has the methods to do a testFail(..) method for a unit test.
Copyright (c) 2024-2025 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See for project information https://djutils.org. The DJUTILS project is distributed under a three-clause BSD-style license, which can be found at https://djutils.org/docs/license.html.
- Author:
- Alexander Verbraeck, Peter Knoppers, Wouter Schakel
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
Functional interface for calls to Try.assign(...).static interface
Functional interface for calls to Try.execute(...). -
Method Summary
Modifier and TypeMethodDescriptionstatic <V> V
testFail
(UnitTest.Assignment<V> assignment) Method for unit tests to test if an expected exception is thrown on an assignment.static <V,
T extends Throwable>
VtestFail
(UnitTest.Assignment<V> assignment, Class<T> expectedThrowableClass) Method for unit tests to test if an expected exception is thrown on an assignment.static <V> V
testFail
(UnitTest.Assignment<V> assignment, String message) Method for unit tests to test if an expected exception is thrown on an assignment.static <V,
T extends Throwable>
VtestFail
(UnitTest.Assignment<V> assignment, String message, Class<T> expectedThrowableClass) Method for unit tests to test if an expected exception is thrown on an assignment.static void
testFail
(UnitTest.Execution execution) Method for unit tests to test if an expected exception is thrown on code execution.static <T extends Throwable>
voidtestFail
(UnitTest.Execution execution, Class<T> expectedThrowableClass) Method for unit tests to test if an expected exception is thrown on code execution.static void
testFail
(UnitTest.Execution execution, String message) Method for unit tests to test if an expected exception is thrown on code execution.static <T extends Throwable>
voidtestFail
(UnitTest.Execution execution, String message, Class<T> expectedThrowableClass) Method for unit tests to test if an expected exception is thrown on code execution.
-
Method Details
-
testFail
Method for unit tests to test if an expected exception is thrown on an assignment. This method does not provide an explanation, and it is not checking for a specific type of exception to be thrown. The testFail() method throws an AssertionError when the assignment does not throw any exception. A way to use the method is, for instance:
UnitTest.testFail(() -> methodFailsOnNull(null));
UnitTest.testFail(new Try.Assignment<Double>() { @Override public Double assign() throws Throwable { return methodFailsOnNull(null); } });
- Type Parameters:
V
- value type, which is the return type of the assignment- Parameters:
assignment
- functional interface to assign value- Returns:
- assigned value
- Throws:
AssertionError
- when the assignment fails to throw an exception
-
testFail
Method for unit tests to test if an expected exception is thrown on an assignment. This method provides an explanation message, but it is not checking for a specific type of exception to be thrown. The testFail() method throws an AssertionError when the assignment does not throw an exception. A way to use the method is, for instance:
UnitTest.testFail(() -> methodFailsOnNull(null), "call should have thrown an NPE");
UnitTest.testFail(new Try.Assignment<Double>() { @Override public Double assign() throws Throwable { return methodFailsOnNull(null); } }, "call should have thrown an NPE");
- Type Parameters:
V
- value type, which is the return type of the assignment- Parameters:
assignment
- functional interface to assign valuemessage
- message to use in the AssertionError when the assignment succeeds- Returns:
- assigned value
- Throws:
AssertionError
- when the assignment fails to throw an exception
-
testFail
public static <V,T extends Throwable> V testFail(UnitTest.Assignment<V> assignment, Class<T> expectedThrowableClass) Method for unit tests to test if an expected exception is thrown on an assignment. This method does not provide an explanation, but it is checking for a specific type of exception to be thrown. The testFail() method throws an AssertionError when the assignment does not throw an exception, or when it throws a different exception than expectedThrowableClass. A way to use the method is, for instance:
UnitTest.testFail(() -> methodFailsOnNull(null), NullPointerException.class);
UnitTest.testFail(new Try.Assignment<Double>() { @Override public Double assign() throws Throwable { return methodFailsOnNull(null); } }, NullPointerException.class);
- Type Parameters:
V
- value type, which is the return type of the assignmentT
- throwable type, which ensures that we provide a throwable class as the argument- Parameters:
assignment
- functional interface to assign valueexpectedThrowableClass
- the class of the exception we expect the assignment to throw- Returns:
- assigned value
- Throws:
AssertionError
- when the assignment fails to throw an exception or the correct exception
-
testFail
public static <V,T extends Throwable> V testFail(UnitTest.Assignment<V> assignment, String message, Class<T> expectedThrowableClass) Method for unit tests to test if an expected exception is thrown on an assignment. This method provides an explanation message, and it is checking for a specific type of exception to be thrown. The testFail() method throws an AssertionError when the assignment does not throw an exception, or when it throws a different exception than expectedThrowableClass. A way to use the method is, for instance:
UnitTest.testFail(() -> methodFailsOnNull(null), "call should have thrown an NPE", NullPointerException.class);
UnitTest.testFail(new Try.Assignment<Double>() { @Override public Double assign() throws Throwable { return methodFailsOnNull(null); } }, "call should have thrown an NPE", NullPointerException.class);
- Type Parameters:
V
- value type, which is the return type of the assignmentT
- throwable type, which ensures that we provide a throwable class as the argument- Parameters:
assignment
- functional interface to assign valuemessage
- message to use in the AssertionError when the test failsexpectedThrowableClass
- the class of the exception we expect the assignment to throw- Returns:
- assigned value
-
testFail
Method for unit tests to test if an expected exception is thrown on code execution. This method does not provide an explanation message, nor is it checking for a specific type of exception to be thrown. The testFail() method throws an AssertionError when the execution does not throw an exception. A way to use the method is, for instance:
UnitTest.testFail(() -> methodFailsOnNull(null));
UnitTest.testFail(new Try.Execution() { @Override public void execute() throws Throwable { methodFailsOnNull(null); } });
- Parameters:
execution
- functional interface to execute a method that does not need to return a value
-
testFail
Method for unit tests to test if an expected exception is thrown on code execution. This method provides an explanation message, but it is not checking for a specific type of exception to be thrown. The testFail() method throws an AssertionError when the execution does not throw an exception, or when it throws a different exception than expectedThrowableClass. A way to use the method is, for instance:
UnitTest.testFail(() -> methodFailsOnNull(null), "call should have thrown an NPE");
UnitTest.testFail(new Try.Execution() { @Override public void execute() throws Throwable { methodFailsOnNull(null); } }, "call should have thrown an NPE");
- Parameters:
execution
- functional interface to execute a method that does not need to return a valuemessage
- message to use in the AssertionError when the test fails
-
testFail
public static <T extends Throwable> void testFail(UnitTest.Execution execution, Class<T> expectedThrowableClass) Method for unit tests to test if an expected exception is thrown on code execution. This method does not provide an explanation message, but it is checking for a specific type of exception to be thrown. The testFail() method throws an AssertionError when the execution does not throw an exception, or when it throws a different exception than expectedThrowableClass. A way to use the method is, for instance:
UnitTest.testFail(() -> methodFailsOnNull(null), NullPointerException.class);
UnitTest.testFail(new Try.Execution() { @Override public void execute() throws Throwable { methodFailsOnNull(null); } }, NullPointerException.class);
- Type Parameters:
T
- throwable type, which ensures that we provide a throwable class as the argument- Parameters:
execution
- functional interface to execute a method that does not need to return a valueexpectedThrowableClass
- the class of the exception we expect the execution to throw
-
testFail
public static <T extends Throwable> void testFail(UnitTest.Execution execution, String message, Class<T> expectedThrowableClass) Method for unit tests to test if an expected exception is thrown on code execution. This method provides an explanation message, and it is checking for a specific type of exception to be thrown. The testFail() method throws an AssertionError when the execution does not throw an exception, or when it throws a different exception than expectedThrowableClass. A way to use the method is, for instance:
UnitTest.testFail(() -> methodFailsOnNull(null), "call should have thrown an NPE", NullPointerException.class);
UnitTest.testFail(new Try.Execution() { @Override public void execute() throws Throwable { methodFailsOnNull(null); } }, "call should have thrown an NPE", NullPointerException.class);
- Type Parameters:
T
- throwable type, which ensures that we provide a throwable class as the argument- Parameters:
execution
- functional interface to execute a method that does not need to return a valuemessage
- message to use in the AssertionError when the test failsexpectedThrowableClass
- the class of the exception we expect the execution to throw
-