WARNING: This server provides a static reference view of the NetKernel documentation. Links to dynamic content do not work. For the best experience we recommend you install NetKernel and view the documentation in the live system .

Custom Asserts

In addition to the built-in assertions, custom assertions may be defined within a testlist. A custom assertion is a declarative request that uses the outer element assertDefinition and includes a required name attribute.

<assertDefinition name="validCustomer"> ... </assertDefinition>

The value of the name attribute can be used in the assert part of a unit test declaration as the name of the assertion:

<assert>
  <validCustomer />
</assert>

Because asserts are used to examine the representation returned by a unit test request, information about the test result must be provided to the assert declarative request. This is accomplished by using an arg: reference when specifying the arguments in the assert request. For example, the following custom assert uses the active:groovy service to run a Groovy language program that accepts the named argument "result":

<assertDefinition name="validCustomer">
  <identifier>active:groovy</identifier>
  <argument name="operator">res:/.../validCustomerAssertion.gy</argument>
  <argument name="result">arg:test:result</argument>
</assertDefinition>

The XUnit test manager provides the following information from the response of a test request:

  • arg:test:response - the INKFResponseReadOnly object returned by the test request
  • arg:test:result - the representation object returned by the test request.
  • arg:test:tagValue - the value of the assert tag's text as a String object or may use syntax (see below).
  • arg:test:tagRef - the value of the assert tag's text as a pass-by-reference argument identifier.

When arg:test:tagRef is used the value of the tag's text is passed by-reference. When the arg:test:tagValue is used the value of the tag's test is passed as a String object by-value. Both arg:test:response and arg:test:result are passed by-value.

Example

This custom assert defines the "lessThan" assertion that will evaluate to true when the test request returns a representation that is an instance of the class Integer and its value is less than the value in the assert tag's text.

<assertDefinition name="lessThan">
  <base>active:groovy</base>
  <argument name="operator">res:/test/assertLessThan.gy</argument>
  <argument name="result">arg:test:response</argument>
  <argument name="testvalue">arg:test:tagValue</argument>
</assertDefinition>

The Groovy program that implements the lessThan assertion is:

// Asserts that the response has a java.lang.Integer with a value less than the specified value.

representation = context.source("arg:result").getRepresentation()

valid = false
if (representation instanceof java.lang.Integer)
  {
  try
    {
    test = context.source("arg:testvalue")
    if (representation < Integer.parseInt(test))
     {
      valid = true
     }
     else
     {  //Report expected/received
        context.sink("active:assert/Expected", test)
        context.sink("active:assert/Received", Integer.toString(representation))
     }
   }
  catch(Exception e)
    {
    // We had some sort of String to Integer conversion error
    valid = false
    }
  }
response = context.createResponseFrom(valid)
response.setExpired()

The lessThan assert could be used in a test as follows:

<test> ...
  <assert>
    <lessThan>256</lessThan>
  </assert>
</test>

Literals

By default the value of the custom assert tag is treated as a string and passed as the arg:test:tagValue. However the value of a custom assert tag may be a using the literal parser, as used with declarative requests.

A tag will automatically be detected and parsed to the specified representation.

For example if you wished to pass a literal boolean as the assert you could do this..

<assert>
  <myCustomAssert>
    <literal type="boolean">true</literal>
  </myCustomAssert>
</assert>

Literals support several standard types but may also invoke new instances of classes. The referenced class must be accessable in the classpath of the module that defines the unit tests.

For example here is an assert with a literal URI object...

<assert>
  <myCustomAssert>
    <literal type="java.net.URI">
      <literal type="string">http://www.1060.org/</literal>
    </literal>
  </myCustomAssert>
</assert>

For details of types and POJO representations see the declarative request reference documentation.

Expected/Received

Upon an assertion failure a custom assert may report the expected and received representation state by SINKing to

active:assert/Expected
active:assert/Received

These representations must be transreptable to java.lang.String