Class JxnUnitTest

  • Direct Known Subclasses:
    JxnUnitTestFailureExpected

    public class JxnUnitTest
    extends Object
    Provides a set of methods supporting unit tests in JXN (similar to → JUnit.org).

    The following example illustrates how to use JxnUnitTest in JXN:
    
       ut = @JxnUnitTest();
       !
       ! Passed Tests:
       ut.assertEquals( 3.14159, PI, 0.00001 )
        = ok (java.lang.String)
       ut.assertEquals( 1.414, sqrt 2, 0.001 )
        = ok (java.lang.String)
       !
       ! Failed Test:
       ut.assertEquals( 1.414, sqrt 2, 0.0001 )
        = **failed** expected: 1.414, found: 1.4142135623730951 (java.lang.String)
       !
       ! The following test throws an exception because the method mySquare(double) is not (yet) defined:
       ! $this.test "..." catches the Exception, to avoid that JXN terminates. Instead it returns the exception object.
       ut.assertEquals( 9., $this.test "mySquare(3.)" )
       ? invokeMethod: identifier mySquare( double ) undefined, wrong number of parameters or not public
        = **failed** expected: 9.0 (double), found: KmgFormelException: invokeMethod: identifier mySquare( double ) undefined, wrong number of parameters or not public (class KmgFormelException) (java.lang.String)
       !
       ! Expected Exception:
       expected = "java.lang.Integer.parseInt( java.lang.String ): java.lang.NumberFormatException...";
       ut.assertException( expected, $this.test "Integer.parseInt( \"1.5\" )" )
       ? java.lang.Integer.parseInt( java.lang.String ): java.lang.NumberFormatException: For input string: "1.5"
        = ok (expected exception) (java.lang.String)
       !
       ut.summary()
        = ***** 2 of 5 tests failed ***** (java.lang.String)
    
    
    Note: JxnUnitTest is intended for moderate-size unit tests with minimum overhead. It can be used interactively to execute and record tests on the fly. Big scale unit tests including integration of automatic regression tests in the build process should consider to use → JUnit.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static boolean ID  
      protected double itsDoubleEps
      tolerance for double comparison.
      protected float itsFloatEps
      tolerance for float comparison.
    • Method Detail

      • reset

        public String reset()
        Resets the test counters of this instance.
      • getDoubleEps

        public double getDoubleEps()
        Returns the itsDoubleEps value used for this instance.
      • getFloatEps

        public float getFloatEps()
        Returns the itsFloatEps value used for this instance.
      • getDoubleMaxDiff

        public double getDoubleMaxDiff()
        Returns the maximum difference observed for double comparison.
      • getFloatMaxDiff

        public float getFloatMaxDiff()
        Returns the maximum difference observed for float comparison.
      • setShowHashCode

        public static boolean setShowHashCode​(boolean show)
        Sets show hash code in messages.
      • setVerbose

        public static boolean setVerbose​(boolean vb)
        Sets degree of detail in messages.
      • setIgnore

        public JxnUnitTest setIgnore​(boolean ignore)
        Tests between ut.setIgnore(true) and ut.setIgnore(false) are excluded from the failure counters.
      • ignore

        public JxnUnitTest ignore()
        Ignores the following single test. Example:
            ut.ignore().assertEquals( 3.14159, PI, 0.00001 )
         
      • setOkString

        public JxnUnitTest setOkString​(String text)
        Sets the String returned for passed tests.
      • setFailString

        public JxnUnitTest setFailString​(String text)
        Sets the String returned for failed tests.
      • setOkIgnore

        public JxnUnitTest setOkIgnore​(String text)
        Sets the String returned for ignored tests which pass.
      • setFailIgnore

        public JxnUnitTest setFailIgnore​(String text)
        Sets the String returned for ignored tests which fail.
      • ok

        public String ok()
        Returns the String returned for passed test.
      • failed

        public String failed()
        Returns the String returned for failed test.
      • updateTest

        protected void updateTest()
        Updates counters (for all tests).
      • updateFail

        protected void updateFail()
        Updates counters for failed tests.
      • summary

        public String summary()
        Returns summary as String.
      • summary

        public String summary​(boolean doReset)
        Returns summary as String and resets test counters of this instance, if doReset is true.
      • summaryAll

        public static String summaryAll​(boolean doReset)
        Returns summary of all tests as String and resets all static test counters, if doReset is true.
      • toString

        public static String toString​(Object o)
        Formats o as String.
      • toString

        public static String toString​(Object o,
                                      boolean showHashCode)
        Formats o as String.
      • assertException

        public String assertException​(String msg,
                                      Object actual)
        Not in JUnit - Asserts that a test throws a specific exception.
          ! expected exception not specified:
          ut.assertException( "...", $this.test( "Integer.parseInt( \"123.\" )" ) )
           = ok (expected exception) (java.lang.String)
        
          ! short message text:
          ut_exp = "java.lang.Integer.parseInt( java.lang.String ): java.lang.NumberFormatException: ...";
          ut.assertException( ut_exp, $this.test( "Integer.parseInt( \"123.\" )" ) )
           = ok (expected exception) (java.lang.String)
         
          ! full message text:
          ut_exp = "java.lang.Integer.parseInt( java.lang.String ): java.lang.NumberFormatException: For input string: \"123.\"";
          ut.assertException( ut_exp, $this.test( "Integer.parseInt( \"123.\" )" ) )
           = ok (expected exception) (java.lang.String)
         
        Parameters:
        msg - text of the expected exception. The text can be abreviated using "...". See the examples above.
        actual - for passing tests the expected exception
      • assertException

        public String assertException​(String msg,
                                      double actual)
        Not in JUnit - Special signature ⇒ always fails (not throwing an exception though expected).
        See Also:
        assertException(String,Object)
      • assertException

        public String assertException​(String msg,
                                      long actual)
        Not in JUnit - Special signature ⇒ always fails (not throwing an exception though expected).
        See Also:
        assertException(String,Object)
      • assertException

        public String assertException​(String msg,
                                      boolean actual)
        Not in JUnit - Special signature ⇒ always fails (not throwing an exception though expected).
        See Also:
        assertException(String,Object)
      • assertException

        public String assertException​(String msg,
                                      char actual)
        Not in JUnit - Special signature ⇒ always fails (not throwing an exception though expected).
        See Also:
        assertException(String,Object)
      • assertTrue

        public String assertTrue​(Object actual)
        Not in JUnit - Special signature ⇒ always fails (called, if the test expected to return true initially throws an exception).
      • assertFalse

        public String assertFalse​(Object actual)
        Not in JUnit - Special signature ⇒ always fails (called, if the test expected to return false initially throws an exception).
      • assertEquivalent

        public String assertEquivalent​(Object expected,
                                       Object actual)
        Not in JUnit - Asserts that the String representations of two objects are equal.
        If expected is an array, the string representations of corresponding elements of both arrays must be equal.
      • assertArrayEquals

        protected String assertArrayEquals​(Object expected,
                                           Object actual,
                                           int mode)
        Not in JUnit - Asserts that two arrays are equal (used internally).
        Parameters:
        mode -
        if mode == 0, the array components are compared using the equals method of the component objects
        if mode == 1, the string representations of the array components are compared
        if mode == -1, the string representations of the array components are checked: actual[i].startsWith( expected[i] )
      • assertEquals

        public String assertEquals​(double[] expected,
                                   double[] actual,
                                   double delta,
                                   boolean relative)
        Not in JUnit - Asserts that two double arrays are equal with absolute or relative tolerance.
        Parameters:
        delta - tolerance for double comparison
        relative -
        if true, delta * Math.max( 1., expected[i] ) is used as maximum deviation allowed (relative tolerance)
        if false, delta is used as maximum deviation allowed (absolute tolerance)
      • assertEquals

        public String assertEquals​(float[] expected,
                                   float[] actual,
                                   float delta,
                                   boolean relative)
        Not in JUnit - Asserts that two float arrays are equal with absolute or relative tolerance.
        Parameters:
        delta - tolerance for float comparison
        relative -
        if true, delta * Math.max( 1.f, expected[i] ) is used as maximum deviation allowed (relative tolerance)
        if false, delta is used as maximum deviation allowed (absolute tolerance)
      • assertStartsWith

        public String assertStartsWith​(String expected,
                                       String actual)
        Not in JUnit - Asserts that actual.startsWith(expected).
      • assertStartsWith

        public String assertStartsWith​(String expected,
                                       String actual,
                                       boolean verbose)
        Not in JUnit - Asserts that actual.startsWith(expected).
        Parameters:
        verbose - if true, returns actual for passed tests
      • assertStartsWith

        public String assertStartsWith​(Object expected,
                                       Object actual)
        Not in JUnit - Asserts that actual.startsWith(expected).
      • assertStartsWith

        public String assertStartsWith​(Object expected,
                                       Object actual,
                                       boolean verbose)
        Not in JUnit - Asserts that actual.startsWith(expected).
        Parameters:
        verbose - if true, returns actual for passed tests
      • arrayStartsWith

        protected String arrayStartsWith​(Object expected,
                                         Object actual,
                                         boolean verbose)
        Not in JUnit - Asserts that actual.startsWith(expected) (used internally).
      • assertEquals

        public String assertEquals​(double expected,
                                   double actual,
                                   double delta)
        Same as JUnit - Asserts that two doubles are equal to within a positive delta.

        Parameters:
        delta - may be given as:
        delta = eps (maximum absolute deviation allowed)
        delta = eps * expected (maximim relative deviation allowed)
        delta = 0. (no deviation allowed)

        delta < 0. ⇒ displays relative difference -1. <= diff <= 1. for passed tests.

        See Also:
        org.junit.Assert#assertEquals(double,double,double)
      • assertEquals

        public String assertEquals​(double expected,
                                   Object actual)
        Not in JUnit - Special signature ⇒ always fails (called, if the test expected to return a primitive initially throws an exception).
      • assertEquals

        public String assertEquals​(double expected,
                                   Object actual,
                                   double delta)
        Not in JUnit - Special signature ⇒ always fails (called, if the test expected to return a primitive initially throws an exception).
      • assertEquals

        public String assertEquals​(float expected,
                                   Object actual)
        Not in JUnit - Special signature ⇒ always fails (called, if the test expected to return a primitive initially throws an exception).
      • assertEquals

        public String assertEquals​(float expected,
                                   Object actual,
                                   float delta)
        Not in JUnit - Special signature ⇒ always fails (called, if the test expected to return a primitive initially throws an exception).
      • assertEquals

        public String assertEquals​(long expected,
                                   Object actual)
        Not in JUnit - Special signature ⇒ always fails (called, if the test expected to return a primitive initially throws an exception).
      • assertEquals

        public String assertEquals​(char expected,
                                   Object actual)
        Not in JUnit - Special signature ⇒ always fails (called, if the test expected to return a primitive initially throws an exception).
      • main

        public static void main​(String[] args)