Class JxnUnitTest
- java.lang.Object
- 
- 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 useJxnUnitTestin 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:JxnUnitTestis 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 SummaryFields Modifier and Type Field Description static booleanIDprotected doubleitsDoubleEpstolerance for double comparison.protected floatitsFloatEpstolerance for float comparison.
 - 
Constructor SummaryConstructors Constructor Description JxnUnitTest()The assert methods are called on instances of JxnUnitTest.JxnUnitTest(double doubleEps)Creates instance with presetitsDoubleEpsvalue.JxnUnitTest(double doubleEps, float floatEps)Creates instance with presetitsDoubleEpsanditsFloatEpsvalue.
 - 
Method SummaryAll Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected StringarrayStartsWith(Object expected, Object actual, boolean verbose)Not in JUnit - Asserts thatactual.startsWith(expected)(used internally).protected StringassertArrayEquals(Object expected, Object actual, int mode)Not in JUnit - Asserts that two arrays are equal (used internally).StringassertEquals(char expected, char actual)Not in JUnit - Asserts that two chars are equal.StringassertEquals(char expected, Object actual)Not in JUnit - Special signature ⇒ always fails (called, if the test expected to return a primitive initially throws an exception).StringassertEquals(double[] expected, double[] actual)Not in JUnit - Asserts that two double arrays are equal with relative tolerance usingitsDoubleEps.StringassertEquals(double[] expected, double[] actual, double delta)Same as JUnit - Asserts that two double arrays are equal with absolute tolerancedelta.StringassertEquals(double[] expected, double[] actual, double delta, boolean relative)Not in JUnit - Asserts that two double arrays are equal with absolute or relative tolerance.StringassertEquals(double expected, double actual)Differs from JUnit - Asserts that two doubles are equal with relative tolerance usingitsDoubleEps.StringassertEquals(double expected, double actual, double delta)Same as JUnit - Asserts that two doubles are equal to within a positive delta.StringassertEquals(double expected, Object actual)Not in JUnit - Special signature ⇒ always fails (called, if the test expected to return a primitive initially throws an exception).StringassertEquals(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).StringassertEquals(float[] expected, float[] actual)Not in JUnit - Asserts that two float arrays are equal with relative tolerance usingitsFloatEps.StringassertEquals(float[] expected, float[] actual, float delta)Same as JUnit - Asserts that two float arrays are equal with absolute tolerancedelta.StringassertEquals(float[] expected, float[] actual, float delta, boolean relative)Not in JUnit - Asserts that two float arrays are equal with absolute or relative tolerance.StringassertEquals(float expected, float actual)Not in JUnit - Asserts that two floats are equal with relative tolerance usingitsFloatEps.StringassertEquals(float expected, float actual, float delta)Same as JUnit - Asserts that two floats are equal to within a positive delta.StringassertEquals(float expected, Object actual)Not in JUnit - Special signature ⇒ always fails (called, if the test expected to return a primitive initially throws an exception).StringassertEquals(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).StringassertEquals(long expected, long actual)Same as JUnit - Asserts that twolongs are equal.StringassertEquals(long expected, Object actual)Not in JUnit - Special signature ⇒ always fails (called, if the test expected to return a primitive initially throws an exception).StringassertEquals(Object expected, Object actual)Same as JUnit - Asserts that two objects are equal.StringassertEquivalent(Object expected, Object actual)Not in JUnit - Asserts that the String representations of two objects are equal.StringassertException(String msg, boolean actual)Not in JUnit - Special signature ⇒ always fails (not throwing an exception though expected).StringassertException(String msg, char actual)Not in JUnit - Special signature ⇒ always fails (not throwing an exception though expected).StringassertException(String msg, double actual)Not in JUnit - Special signature ⇒ always fails (not throwing an exception though expected).StringassertException(String msg, long actual)Not in JUnit - Special signature ⇒ always fails (not throwing an exception though expected).StringassertException(String msg, Object actual)Not in JUnit - Asserts that a test throws a specific exception.StringassertFalse(boolean condition)Same as JUnit - Asserts that condition is true.StringassertFalse(Object actual)Not in JUnit - Special signature ⇒ always fails (called, if the test expected to return false initially throws an exception).StringassertNotNull(Object o)Same as JUnit - Asserts that an object isn't null.StringassertNotSame(Object unexpected, Object actual)Same as JUnit - Asserts that two objects do not refer to the same object.StringassertNull(Object o)Same as JUnit - Asserts that an object is null.StringassertSame(Object expected, Object actual)Same as JUnit - Asserts that two objects refer to the same object.StringassertStartsWith(Object expected, Object actual)Not in JUnit - Asserts thatactual.startsWith(expected).StringassertStartsWith(Object expected, Object actual, boolean verbose)Not in JUnit - Asserts thatactual.startsWith(expected).StringassertStartsWith(String expected, String actual)Not in JUnit - Asserts thatactual.startsWith(expected).StringassertStartsWith(String expected, String actual, boolean verbose)Not in JUnit - Asserts thatactual.startsWith(expected).StringassertTrue(boolean condition)Same as JUnit - Asserts that condition is true.StringassertTrue(Object actual)Not in JUnit - Special signature ⇒ always fails (called, if the test expected to return true initially throws an exception).static StringclearAll()Deprecated.replaced byresetAll()Stringfail()Same as JUnit - Always fails.Stringfailed()Returns the String returned for failed test.doublegetDoubleEps()Returns theitsDoubleEpsvalue used for this instance.doublegetDoubleMaxDiff()Returns the maximum difference observed for double comparison.floatgetFloatEps()Returns theitsFloatEpsvalue used for this instance.floatgetFloatMaxDiff()Returns the maximum difference observed for float comparison.JxnUnitTestignore()Ignores the following single test.static voidmain(String[] args)Stringok()Returns the String returned for passed test.Stringreset()Resets the test counters of this instance.static StringresetAll()Resets all static test counters.doublesetDoubleEps(double eps)Sets toleranceitsDoubleEpsfor double value comparison.doublesetEps(double eps)Sets toleranceitsDoubleEpsfor double value comparison.JxnUnitTestsetFailIgnore(String text)Sets the String returned for ignored tests which fail.JxnUnitTestsetFailString(String text)Sets the String returned for failed tests.floatsetFloatEps(float eps)Sets toleranceitsFloatEpsfor float value comparison.JxnUnitTestsetIgnore(boolean ignore)Tests betweenut.setIgnore(true)andut.setIgnore(false)are excluded from the failure counters.JxnUnitTestsetOkIgnore(String text)Sets the String returned for ignored tests which pass.JxnUnitTestsetOkString(String text)Sets the String returned for passed tests.static booleansetShowHashCode(boolean show)Sets show hash code in messages.static booleansetVerbose(boolean vb)Sets degree of detail in messages.Stringsummary()Returns summary as String.Stringsummary(boolean doReset)Returns summary as String and resets test counters of this instance, ifdoResetis true.static StringsummaryAll()Returns summary of all tests as String.static StringsummaryAll(boolean doReset)Returns summary of all tests as String and resets all static test counters, ifdoResetis true.static StringtoString(Object o)Formats o as String.static StringtoString(Object o, boolean showHashCode)Formats o as String.protected voidupdateFail()Updates counters for failed tests.protected voidupdateTest()Updates counters (for all tests).
 
- 
- 
- 
Field Detail- 
IDpublic static boolean ID 
 - 
itsDoubleEpsprotected double itsDoubleEps tolerance for double comparison.
 SeeassertEquals(double, double)andassertEquals(double[], double[])how theitsDoubleEpsvalue ist used.
 - 
itsFloatEpsprotected float itsFloatEps tolerance for float comparison.
 SeeassertEquals(float, float)andassertEquals(float[], float[])how theitsFloatEpsvalue ist used.
 
- 
 - 
Constructor Detail- 
JxnUnitTestpublic JxnUnitTest() The assert methods are called on instances of JxnUnitTest. Multiple instances allow separate counters for different aspects to be tested.
 - 
JxnUnitTestpublic JxnUnitTest(double doubleEps) Creates instance with presetitsDoubleEpsvalue.
 SeeassertEquals( double, double )andassertEquals( double[], double[] )how theitsDoubleEpsvalue is used.
 - 
JxnUnitTestpublic JxnUnitTest(double doubleEps, float floatEps)Creates instance with presetitsDoubleEpsanditsFloatEpsvalue.
 SeeassertEquals( double, double )andassertEquals( double[], double[] )how theitsDoubleEpsvalue is used.
 SeeassertEquals( float, float )andassertEquals( float[], float[] )how theitsFloatEpsvalues is used.
 
- 
 - 
Method Detail- 
resetpublic String reset() Resets the test counters of this instance.
 - 
resetAllpublic static String resetAll() Resets all static test counters.
 Note: IfJxnUnitTestFailureExpectedis used, callJxnUnitTestFailureExpected.resetAll()instead ofJxnUnitTest.resetAll()
 - 
clearAllpublic static String clearAll() Deprecated.replaced byresetAll()
 - 
setEpspublic double setEps(double eps) Sets toleranceitsDoubleEpsfor double value comparison. setEps is a shortcut forsetDoubleEps(double).
 - 
setDoubleEpspublic double setDoubleEps(double eps) Sets toleranceitsDoubleEpsfor double value comparison.
 SeeassertEquals( double, double )andassertEquals( double[], double[] )how theitsDoubleEpsvalue is used.
 - 
setFloatEpspublic float setFloatEps(float eps) Sets toleranceitsFloatEpsfor float value comparison.
 SeeassertEquals( float, float )andassertEquals( float[], float[] )how theitsFloatEpsvalue is used.
 - 
getDoubleEpspublic double getDoubleEps() Returns theitsDoubleEpsvalue used for this instance.
 - 
getFloatEpspublic float getFloatEps() Returns theitsFloatEpsvalue used for this instance.
 - 
getDoubleMaxDiffpublic double getDoubleMaxDiff() Returns the maximum difference observed for double comparison.
 - 
getFloatMaxDiffpublic float getFloatMaxDiff() Returns the maximum difference observed for float comparison.
 - 
setShowHashCodepublic static boolean setShowHashCode(boolean show) Sets show hash code in messages.
 - 
setVerbosepublic static boolean setVerbose(boolean vb) Sets degree of detail in messages.
 - 
setIgnorepublic JxnUnitTest setIgnore(boolean ignore) Tests betweenut.setIgnore(true)andut.setIgnore(false)are excluded from the failure counters.
 - 
ignorepublic JxnUnitTest ignore() Ignores the following single test. Example:ut.ignore().assertEquals( 3.14159, PI, 0.00001 )
 - 
setOkStringpublic JxnUnitTest setOkString(String text) Sets the String returned for passed tests.
 - 
setFailStringpublic JxnUnitTest setFailString(String text) Sets the String returned for failed tests.
 - 
setOkIgnorepublic JxnUnitTest setOkIgnore(String text) Sets the String returned for ignored tests which pass.
 - 
setFailIgnorepublic JxnUnitTest setFailIgnore(String text) Sets the String returned for ignored tests which fail.
 - 
okpublic String ok() Returns the String returned for passed test.
 - 
failedpublic String failed() Returns the String returned for failed test.
 - 
updateTestprotected void updateTest() Updates counters (for all tests).
 - 
updateFailprotected void updateFail() Updates counters for failed tests.
 - 
summarypublic String summary() Returns summary as String.
 - 
summarypublic String summary(boolean doReset) Returns summary as String and resets test counters of this instance, ifdoResetis true.
 - 
summaryAllpublic static String summaryAll() Returns summary of all tests as String.
 Note: IfJxnUnitTestFailureExpectedis used, callJxnUnitTestFailureExpected.summaryAll()instead ofJxnUnitTest.summaryAll()
 - 
summaryAllpublic static String summaryAll(boolean doReset) Returns summary of all tests as String and resets all static test counters, ifdoResetis true.
 - 
assertExceptionpublic 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
 
 - 
assertExceptionpublic String assertException(String msg, double actual) Not in JUnit - Special signature ⇒ always fails (not throwing an exception though expected).- See Also:
- assertException(String,Object)
 
 - 
assertExceptionpublic String assertException(String msg, long actual) Not in JUnit - Special signature ⇒ always fails (not throwing an exception though expected).- See Also:
- assertException(String,Object)
 
 - 
assertExceptionpublic String assertException(String msg, boolean actual) Not in JUnit - Special signature ⇒ always fails (not throwing an exception though expected).- See Also:
- assertException(String,Object)
 
 - 
assertExceptionpublic String assertException(String msg, char actual) Not in JUnit - Special signature ⇒ always fails (not throwing an exception though expected).- See Also:
- assertException(String,Object)
 
 - 
assertTruepublic String assertTrue(boolean condition) Same as JUnit - Asserts that condition is true.- See Also:
- org.junit.Assert#assertTrue(boolean)
 
 - 
assertTruepublic String assertTrue(Object actual) Not in JUnit - Special signature ⇒ always fails (called, if the test expected to return true initially throws an exception).
 - 
assertFalsepublic String assertFalse(boolean condition) Same as JUnit - Asserts that condition is true.- See Also:
- org.junit.Assert#assertFalse(boolean)
 
 - 
assertFalsepublic String assertFalse(Object actual) Not in JUnit - Special signature ⇒ always fails (called, if the test expected to return false initially throws an exception).
 - 
failpublic String fail() Same as JUnit - Always fails.- See Also:
- org.junit.Assert#fail()
 
 - 
assertEquivalentpublic String assertEquivalent(Object expected, Object actual) Not in JUnit - Asserts that the String representations of two objects are equal.
 Ifexpectedis an array, the string representations of corresponding elements of both arrays must be equal.
 - 
assertEqualspublic String assertEquals(Object expected, Object actual) Same as JUnit - Asserts that two objects are equal.
 Ifexpectedis an array, the array elements are tested usingassertArrayEquals( expected, actual, int mode = 0 ).
 - 
assertArrayEqualsprotected 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- equalsmethod 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] )
 
 - 
assertEqualspublic String assertEquals(double[] expected, double[] actual) Not in JUnit - Asserts that two double arrays are equal with relative tolerance usingitsDoubleEps.
 CallsassertEquals( expected, actual, double delta = itsDoubleEps, boolean relative = true ).- See Also:
- setDoubleEps(double)
 
 - 
assertEqualspublic String assertEquals(double[] expected, double[] actual, double delta) Same as JUnit - Asserts that two double arrays are equal with absolute tolerancedelta.
 CallsassertEquals( expected, actual, delta, boolean relative = false ).- Parameters:
- delta- maximum deviation allowed for double comparison (absolute tolerance)
- See Also:
- org.junit.Assert#assertArrayEquals(double[],double[],double)
 
 - 
assertEqualspublic 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,- deltais used as maximum deviation allowed (absolute tolerance)
 
 - 
assertEqualspublic String assertEquals(float[] expected, float[] actual) Not in JUnit - Asserts that two float arrays are equal with relative tolerance usingitsFloatEps.
 CallsassertEquals( expected, actual, float delta = itsFloatEps, boolean relative = true ).- See Also:
- setFloatEps(float)
 
 - 
assertEqualspublic String assertEquals(float[] expected, float[] actual, float delta) Same as JUnit - Asserts that two float arrays are equal with absolute tolerancedelta.
 CallsassertEquals( expected, actual, delta, boolean relative = false ).- Parameters:
- delta- maximum deviation allowed for float comparison (absolute tolerance)
- See Also:
- org.junit.Assert#assertArrayEquals(float[],float[],float)
 
 - 
assertEqualspublic 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,- deltais used as maximum deviation allowed (absolute tolerance)
 
 - 
assertStartsWithpublic String assertStartsWith(String expected, String actual) Not in JUnit - Asserts thatactual.startsWith(expected).
 - 
assertStartsWithpublic String assertStartsWith(String expected, String actual, boolean verbose) Not in JUnit - Asserts thatactual.startsWith(expected).- Parameters:
- verbose- if true, returns actual for passed tests
 
 - 
assertStartsWithpublic String assertStartsWith(Object expected, Object actual) Not in JUnit - Asserts thatactual.startsWith(expected).
 - 
assertStartsWithpublic String assertStartsWith(Object expected, Object actual, boolean verbose) Not in JUnit - Asserts thatactual.startsWith(expected).- Parameters:
- verbose- if true, returns actual for passed tests
 
 - 
arrayStartsWithprotected String arrayStartsWith(Object expected, Object actual, boolean verbose) Not in JUnit - Asserts thatactual.startsWith(expected)(used internally).
 - 
assertEqualspublic String assertEquals(double expected, double actual) Differs from JUnit - Asserts that two doubles are equal with relative tolerance usingitsDoubleEps.
 CallsassertEquals(double expected, double actual, double delta = itsDoubleEps * Math.max( 1., Math.abs(expected) ) ).- See Also:
- setDoubleEps(double)
 
 - 
assertEqualspublic 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)
 
 - 
assertEqualspublic 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).
 - 
assertEqualspublic 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).
 - 
assertEqualspublic String assertEquals(float expected, float actual) Not in JUnit - Asserts that two floats are equal with relative tolerance usingitsFloatEps.
 CallsassertEquals(float expected, float actual, float delta = itsFloatEps * Math.max( 1.f, Math.abs(expected) ) ).- See Also:
- setFloatEps(float)
 
 - 
assertEqualspublic String assertEquals(float expected, float actual, float delta) Same as JUnit - Asserts that two floats are equal to within a positive delta.- Parameters:
- delta- see- assertEquals(double,double,double)
- See Also:
- org.junit.Assert#assertEquals(float,float,float)
 
 - 
assertEqualspublic 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).
 - 
assertEqualspublic 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).
 - 
assertEqualspublic String assertEquals(long expected, long actual) Same as JUnit - Asserts that twolongs are equal. Also used forint, shortandbyte.- See Also:
- org.junit.Assert#assertEquals(long,long)
 
 - 
assertEqualspublic 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).
 - 
assertEqualspublic String assertEquals(char expected, char actual) Not in JUnit - Asserts that two chars are equal.- See Also:
- org.junit.Assert#assertEquals(char,char)
 
 - 
assertEqualspublic 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).
 - 
assertNotNullpublic String assertNotNull(Object o) Same as JUnit - Asserts that an object isn't null.- See Also:
- org.junit.Assert#assertNotNull(Object)
 
 - 
assertNullpublic String assertNull(Object o) Same as JUnit - Asserts that an object is null.- See Also:
- org.junit.Assert#assertNull(Object)
 
 - 
assertSamepublic String assertSame(Object expected, Object actual) Same as JUnit - Asserts that two objects refer to the same object.
 - 
assertNotSamepublic String assertNotSame(Object unexpected, Object actual) Same as JUnit - Asserts that two objects do not refer to the same object.
 - 
mainpublic static void main(String[] args) 
 
- 
 
-