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 Summary
Fields Modifier and Type Field Description static booleanIDprotected doubleitsDoubleEpstolerance for double comparison.protected floatitsFloatEpstolerance for float comparison.
-
Constructor Summary
Constructors 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 Summary
All 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
-
ID
public static boolean ID
-
itsDoubleEps
protected double itsDoubleEps
tolerance for double comparison.
SeeassertEquals(double, double)andassertEquals(double[], double[])how theitsDoubleEpsvalue ist used.
-
itsFloatEps
protected float itsFloatEps
tolerance for float comparison.
SeeassertEquals(float, float)andassertEquals(float[], float[])how theitsFloatEpsvalue ist used.
-
-
Constructor Detail
-
JxnUnitTest
public JxnUnitTest()
The assert methods are called on instances of JxnUnitTest. Multiple instances allow separate counters for different aspects to be tested.
-
JxnUnitTest
public JxnUnitTest(double doubleEps)
Creates instance with presetitsDoubleEpsvalue.
SeeassertEquals( double, double )andassertEquals( double[], double[] )how theitsDoubleEpsvalue is used.
-
JxnUnitTest
public 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
-
reset
public String reset()
Resets the test counters of this instance.
-
resetAll
public static String resetAll()
Resets all static test counters.
Note: IfJxnUnitTestFailureExpectedis used, callJxnUnitTestFailureExpected.resetAll()instead ofJxnUnitTest.resetAll()
-
clearAll
public static String clearAll()
Deprecated.replaced byresetAll()
-
setEps
public double setEps(double eps)
Sets toleranceitsDoubleEpsfor double value comparison. setEps is a shortcut forsetDoubleEps(double).
-
setDoubleEps
public double setDoubleEps(double eps)
Sets toleranceitsDoubleEpsfor double value comparison.
SeeassertEquals( double, double )andassertEquals( double[], double[] )how theitsDoubleEpsvalue is used.
-
setFloatEps
public float setFloatEps(float eps)
Sets toleranceitsFloatEpsfor float value comparison.
SeeassertEquals( float, float )andassertEquals( float[], float[] )how theitsFloatEpsvalue is used.
-
getDoubleEps
public double getDoubleEps()
Returns theitsDoubleEpsvalue used for this instance.
-
getFloatEps
public float getFloatEps()
Returns theitsFloatEpsvalue 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 betweenut.setIgnore(true)andut.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, ifdoResetis true.
-
summaryAll
public static String summaryAll()
Returns summary of all tests as String.
Note: IfJxnUnitTestFailureExpectedis used, callJxnUnitTestFailureExpected.summaryAll()instead ofJxnUnitTest.summaryAll()
-
summaryAll
public static String summaryAll(boolean doReset)
Returns summary of all tests as String and resets all static test counters, ifdoResetis true.
-
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(boolean condition)
Same as JUnit - Asserts that condition is true.- See Also:
org.junit.Assert#assertTrue(boolean)
-
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(boolean condition)
Same as JUnit - Asserts that condition is true.- See Also:
org.junit.Assert#assertFalse(boolean)
-
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).
-
fail
public String fail()
Same as JUnit - Always fails.- See Also:
org.junit.Assert#fail()
-
assertEquivalent
public 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.
-
assertEquals
public 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 ).
-
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 theequalsmethod 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)
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)
-
assertEquals
public 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)
-
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 comparisonrelative-
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)
-
assertEquals
public 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)
-
assertEquals
public 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)
-
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 comparisonrelative-
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)
-
assertStartsWith
public String assertStartsWith(String expected, String actual)
Not in JUnit - Asserts thatactual.startsWith(expected).
-
assertStartsWith
public String assertStartsWith(String expected, String actual, boolean verbose)
Not in JUnit - Asserts thatactual.startsWith(expected).- Parameters:
verbose- if true, returns actual for passed tests
-
assertStartsWith
public String assertStartsWith(Object expected, Object actual)
Not in JUnit - Asserts thatactual.startsWith(expected).
-
assertStartsWith
public String assertStartsWith(Object expected, Object actual, boolean verbose)
Not in JUnit - Asserts thatactual.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 thatactual.startsWith(expected)(used internally).
-
assertEquals
public 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)
-
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, 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)
-
assertEquals
public String assertEquals(float expected, float actual, float delta)
Same as JUnit - Asserts that two floats are equal to within a positive delta.- Parameters:
delta- seeassertEquals(double,double,double)- See Also:
org.junit.Assert#assertEquals(float,float,float)
-
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, long actual)
Same as JUnit - Asserts that twolongs are equal. Also used forint, shortandbyte.- See Also:
org.junit.Assert#assertEquals(long,long)
-
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, char actual)
Not in JUnit - Asserts that two chars are equal.- See Also:
org.junit.Assert#assertEquals(char,char)
-
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).
-
assertNotNull
public String assertNotNull(Object o)
Same as JUnit - Asserts that an object isn't null.- See Also:
org.junit.Assert#assertNotNull(Object)
-
assertNull
public String assertNull(Object o)
Same as JUnit - Asserts that an object is null.- See Also:
org.junit.Assert#assertNull(Object)
-
assertSame
public String assertSame(Object expected, Object actual)
Same as JUnit - Asserts that two objects refer to the same object.
-
assertNotSame
public String assertNotSame(Object unexpected, Object actual)
Same as JUnit - Asserts that two objects do not refer to the same object.
-
main
public static void main(String[] args)
-
-