Java Reference
In-Depth Information
The function
isTrue(message)
tests that the actual value is
true
, displaying an
optional message if it isn't.
■
The function
isFalse(message)
tests that the actual value is
false
, displaying
an optional message if it isn't.
■
The function
not(predicate)
inverts the predicate.
■
The function
isNull(message)
tests that the actual value is
null
, returning the
message if it isn't.
■
The following example shows how to match regular expressions:
var actual = "JUnit in Action";
assert.that(actual, matches(/in/));
assert.that(actual, not(matches(/out/)));
The example calls the
match
function first to assert that “in” is in the test string
b
and
then to check that “out” isn't
C
.
The
assert
object contains other useful functions:
fail
is like the
JU
nit
fail
method. The various
mustCall
functions check that the tests causing the given func-
tions have or have not been invoked.
In order to run the tests, you'll need an Ant build script. Listing 13.4 is the sample
build script used to run our examples.
B
C
Listing 13.4
Ant build.xml for JavaScript unit tests
<project name="ch13-ajax-rhinounit" basedir="." default="run-all-tests">
<target name="run-all-tests" depends="run-unit-tests, run-js-lint" />
<property name="src.dir" value="src/main/webapp" />
<property name="test.dir" value="src/test/webapp" />
<property name="rhinounit.dir" value="rhinounit_1_2_1" />
<property name="rhinounit.src" value="${rhinounit.dir}/src" />
<property name="JSLint.src" value="${rhinounit.dir}/JSLint" />
B
<!-- Requires Java 6 or Java 5 + BSF -->
<scriptdef name="rhinounit" src="${rhinounit.src}/rhinoUnitAnt.js"
language="javascript">
<attribute name="options" />
<attribute name="ignoredglobalvars" />
<attribute name="haltOnFirstFailure" />
<attribute name="rhinoUnitUtilPath" />
<element name="fileset" type="fileset" />
</scriptdef>
C
<target name="run-unit-tests">
<rhinounit options="{verbose:true, stackTrace:true}"
ignoredglobalvars="rhinounit"
rhinoUnitUtilPath="${rhinounit.src}/rhinoUnitUtil.js">
<fileset dir="${test.dir}">
<include name="*.js" />
</fileset>
</rhinounit>
</target>
D