Class UnitTest

java.lang.Object
org.djutils.test.UnitTest

public final class UnitTest extends Object
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
  • Method Details

    • testFail

      public static <V> V testFail(UnitTest.Assignment<V> assignment)
      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));
       
       
      or
      
         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

      public 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. 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");
       
       
      or
      
         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 value
      message - 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);
       
       
      or
      
         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 assignment
      T - throwable type, which ensures that we provide a throwable class as the argument
      Parameters:
      assignment - functional interface to assign value
      expectedThrowableClass - 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);
       
       
      or
      
         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 assignment
      T - throwable type, which ensures that we provide a throwable class as the argument
      Parameters:
      assignment - functional interface to assign value
      message - message to use in the AssertionError when the test fails
      expectedThrowableClass - the class of the exception we expect the assignment to throw
      Returns:
      assigned value
    • testFail

      public static void testFail(UnitTest.Execution execution)
      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));
       
       
      or
      
         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

      public static void testFail(UnitTest.Execution execution, String message)
      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");
       
       
      or
      
         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 value
      message - 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);
       
       
      or
      
         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 value
      expectedThrowableClass - 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);
       
       
      or
      
         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 value
      message - message to use in the AssertionError when the test fails
      expectedThrowableClass - the class of the exception we expect the execution to throw