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 useJxnUnitTest
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.
-
Constructor Summary
Constructors Constructor Description JxnUnitTest()
The assert methods are called on instances of JxnUnitTest.JxnUnitTest(double doubleEps)
Creates instance with presetitsDoubleEps
value.JxnUnitTest(double doubleEps, float floatEps)
Creates instance with presetitsDoubleEps
anditsFloatEps
value.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected String
arrayStartsWith(Object expected, Object actual, boolean verbose)
Not in JUnit - Asserts thatactual.startsWith(expected)
(used internally).protected String
assertArrayEquals(Object expected, Object actual, int mode)
Not in JUnit - Asserts that two arrays are equal (used internally).String
assertEquals(char expected, char actual)
Not in JUnit - Asserts that two chars are equal.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).String
assertEquals(double[] expected, double[] actual)
Not in JUnit - Asserts that two double arrays are equal with relative tolerance usingitsDoubleEps
.String
assertEquals(double[] expected, double[] actual, double delta)
Same as JUnit - Asserts that two double arrays are equal with absolute tolerancedelta
.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.String
assertEquals(double expected, double actual)
Differs from JUnit - Asserts that two doubles are equal with relative tolerance usingitsDoubleEps
.String
assertEquals(double expected, double actual, double delta)
Same as JUnit - Asserts that two doubles are equal to within a positive delta.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).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).String
assertEquals(float[] expected, float[] actual)
Not in JUnit - Asserts that two float arrays are equal with relative tolerance usingitsFloatEps
.String
assertEquals(float[] expected, float[] actual, float delta)
Same as JUnit - Asserts that two float arrays are equal with absolute tolerancedelta
.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.String
assertEquals(float expected, float actual)
Not in JUnit - Asserts that two floats are equal with relative tolerance usingitsFloatEps
.String
assertEquals(float expected, float actual, float delta)
Same as JUnit - Asserts that two floats are equal to within a positive delta.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).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).String
assertEquals(long expected, long actual)
Same as JUnit - Asserts that twolong
s are equal.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).String
assertEquals(Object expected, Object actual)
Same as JUnit - Asserts that two objects are equal.String
assertEquivalent(Object expected, Object actual)
Not in JUnit - Asserts that the String representations of two objects are equal.String
assertException(String msg, boolean actual)
Not in JUnit - Special signature ⇒ always fails (not throwing an exception though expected).String
assertException(String msg, char actual)
Not in JUnit - Special signature ⇒ always fails (not throwing an exception though expected).String
assertException(String msg, double actual)
Not in JUnit - Special signature ⇒ always fails (not throwing an exception though expected).String
assertException(String msg, long actual)
Not in JUnit - Special signature ⇒ always fails (not throwing an exception though expected).String
assertException(String msg, Object actual)
Not in JUnit - Asserts that a test throws a specific exception.String
assertFalse(boolean condition)
Same as JUnit - Asserts that condition is true.String
assertFalse(Object actual)
Not in JUnit - Special signature ⇒ always fails (called, if the test expected to return false initially throws an exception).String
assertNotNull(Object o)
Same as JUnit - Asserts that an object isn't null.String
assertNotSame(Object unexpected, Object actual)
Same as JUnit - Asserts that two objects do not refer to the same object.String
assertNull(Object o)
Same as JUnit - Asserts that an object is null.String
assertSame(Object expected, Object actual)
Same as JUnit - Asserts that two objects refer to the same object.String
assertStartsWith(Object expected, Object actual)
Not in JUnit - Asserts thatactual.startsWith(expected)
.String
assertStartsWith(Object expected, Object actual, boolean verbose)
Not in JUnit - Asserts thatactual.startsWith(expected)
.String
assertStartsWith(String expected, String actual)
Not in JUnit - Asserts thatactual.startsWith(expected)
.String
assertStartsWith(String expected, String actual, boolean verbose)
Not in JUnit - Asserts thatactual.startsWith(expected)
.String
assertTrue(boolean condition)
Same as JUnit - Asserts that condition is true.String
assertTrue(Object actual)
Not in JUnit - Special signature ⇒ always fails (called, if the test expected to return true initially throws an exception).static String
clearAll()
Deprecated.replaced byresetAll()
String
fail()
Same as JUnit - Always fails.String
failed()
Returns the String returned for failed test.double
getDoubleEps()
Returns theitsDoubleEps
value used for this instance.double
getDoubleMaxDiff()
Returns the maximum difference observed for double comparison.float
getFloatEps()
Returns theitsFloatEps
value used for this instance.float
getFloatMaxDiff()
Returns the maximum difference observed for float comparison.JxnUnitTest
ignore()
Ignores the following single test.static void
main(String[] args)
String
ok()
Returns the String returned for passed test.String
reset()
Resets the test counters of this instance.static String
resetAll()
Resets all static test counters.double
setDoubleEps(double eps)
Sets toleranceitsDoubleEps
for double value comparison.double
setEps(double eps)
Sets toleranceitsDoubleEps
for double value comparison.JxnUnitTest
setFailIgnore(String text)
Sets the String returned for ignored tests which fail.JxnUnitTest
setFailString(String text)
Sets the String returned for failed tests.float
setFloatEps(float eps)
Sets toleranceitsFloatEps
for float value comparison.JxnUnitTest
setIgnore(boolean ignore)
Tests betweenut.setIgnore(true)
andut.setIgnore(false)
are excluded from the failure counters.JxnUnitTest
setOkIgnore(String text)
Sets the String returned for ignored tests which pass.JxnUnitTest
setOkString(String text)
Sets the String returned for passed tests.static boolean
setShowHashCode(boolean show)
Sets show hash code in messages.static boolean
setVerbose(boolean vb)
Sets degree of detail in messages.String
summary()
Returns summary as String.String
summary(boolean doReset)
Returns summary as String and resets test counters of this instance, ifdoReset
is true.static String
summaryAll()
Returns summary of all tests as String.static String
summaryAll(boolean doReset)
Returns summary of all tests as String and resets all static test counters, ifdoReset
is true.static String
toString(Object o)
Formats o as String.static String
toString(Object o, boolean showHashCode)
Formats o as String.protected void
updateFail()
Updates counters for failed tests.protected void
updateTest()
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 theitsDoubleEps
value ist used.
-
itsFloatEps
protected float itsFloatEps
tolerance for float comparison.
SeeassertEquals(float, float)
andassertEquals(float[], float[])
how theitsFloatEps
value 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 presetitsDoubleEps
value.
SeeassertEquals( double, double )
andassertEquals( double[], double[] )
how theitsDoubleEps
value is used.
-
JxnUnitTest
public JxnUnitTest(double doubleEps, float floatEps)
Creates instance with presetitsDoubleEps
anditsFloatEps
value.
SeeassertEquals( double, double )
andassertEquals( double[], double[] )
how theitsDoubleEps
value is used.
SeeassertEquals( float, float )
andassertEquals( float[], float[] )
how theitsFloatEps
values 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: IfJxnUnitTestFailureExpected
is used, callJxnUnitTestFailureExpected.resetAll()
instead ofJxnUnitTest.resetAll()
-
clearAll
public static String clearAll()
Deprecated.replaced byresetAll()
-
setEps
public double setEps(double eps)
Sets toleranceitsDoubleEps
for double value comparison. setEps is a shortcut forsetDoubleEps(double)
.
-
setDoubleEps
public double setDoubleEps(double eps)
Sets toleranceitsDoubleEps
for double value comparison.
SeeassertEquals( double, double )
andassertEquals( double[], double[] )
how theitsDoubleEps
value is used.
-
setFloatEps
public float setFloatEps(float eps)
Sets toleranceitsFloatEps
for float value comparison.
SeeassertEquals( float, float )
andassertEquals( float[], float[] )
how theitsFloatEps
value is used.
-
getDoubleEps
public double getDoubleEps()
Returns theitsDoubleEps
value used for this instance.
-
getFloatEps
public float getFloatEps()
Returns theitsFloatEps
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 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, ifdoReset
is true.
-
summaryAll
public static String summaryAll()
Returns summary of all tests as String.
Note: IfJxnUnitTestFailureExpected
is 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, ifdoReset
is 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.
Ifexpected
is 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.
Ifexpected
is 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 theequals
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)
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,delta
is 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,delta
is 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 twolong
s are equal. Also used forint, short
andbyte
.- 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)
-
-