Tech Blog

Creating BizUnit Test Cases for comparing Xml Files

BizUnit is a great tool for performing end-to-end testing of BizTalk applications
(and can even be used for testing non-BizTalk applications like Web services too).
Although you’re not strictly performing unit-testing of your BizTalk artefacts per-se,
with the right setup you can get very close to unit testing.

For example if you want to test an orchestration in isolation you can:

  1. Hook your orchestration up to file send/receive ports
    or
  2. Write a façade orchestration which calls your orchestration, and is itself hooked
    up to file send/receive ports or
    or
  3. Write your own orchestration hosting engine

I don’t know many people who’d be crazy enough to do 3) (although I do know one…)
but BizUnit is of great help for 1) and 2)

In its simplest form you can use BizUnit to:

1. Copy a file from one location to another (e.g. a location watched by a BizTalk
file receive port)
2. Watch another location for a file to arrive
3. When the file arrives, validate it against an Xsd
4. Optionally check for certain values in the file, using XPath statements

This is a very simple example of using BizUnit, and it assumes that in this scenario
you can test a piece of BizTalk functionality by dropping in an Xml request file and
checking the resultant Xml response file.

If the response file you’re expecting to receive is identical to an expected file,
then you can do a simple binary compare i.e. is every byte in the response file identical
to every byte in the expected file.

However if your response file differs in some way e.g. has some timestamp fields,
or a unique id generated with each response, then you’re left with having to check
only the elements/attributes you expect to be the same.

And this is where XPath comes in.
The XmlValidatinStep (and XmlValidationStepEx)
in BizUnit allow you to specify an arbitrary number of XPath statements and the value
expected for each.
The step then executes each XPath statement on the Xml response file, and compares
the value received to the value expected. If any of them do not match, the test fails.

For example, say you had a request to retrieve all active employees:
<Request #0000ff”>urn:schema.mine.com:requests>

 <Employees status=”Active/>

</Request>

And you expected this response:
<Employees >”urn:schema.mine.com:employees responseTime=2007-11-20T12:31:21>
 <Employee id=1 status=Active>
  <FirstName>Sam</FirstName>
  <LastName>Smith</LastName>
 </Employee>
 <Employee id=2 status=Active>
  <FirstName>Jane</FirstName>
  <LastName>Smith</LastName>
 </Employee
</Employees>

In this case, the response message should contain two Employee elements,
both with a status of Active, with (possibly)
specific names.

However, the response also contains a timestamp (the responseTime attribute)
which changes each time you get a response.
For this reason, you can’t do an exact match against an expected response i.e. you
can’t compare the response file against another file and see if they are an exact
match.

Instead, you need to check for certain fields:
a) Count of Employee elements is 2
b) Employee element at position 1 has an id of 1
c) Employee element at position 1 has a status of ‘active’
d) Employee element at position 2 has an id of 1
e) Employee element at position 2 has a status of ‘active’

This is very easy to do with XPath statements.
For example, to check the count of Employee elements, you would use this XPath:
count(/*[local-name()=’Employees’ and namespace-uri()=”]/*[local-name()=’Employee’
and namespace-uri()=”])

And to check if the element at position 1 has an id of 1 would be:
/*[local-name()=’Employees’ and namespace-uri()=”]/*[local-name()=’Employee’
and namespace-uri()=”][1]/@*[local-name()=’id’ and namespace-uri()=”]
is
equal to 1

In a BizUnit Test Case, under the XmlValidationStep,
you would express this as:
<XPathList>
  <!– Count of Element Employee must equal ‘2’–>

  <XPathValidation query=count(/*[local-name()=’Employees’
and namespace-uri()=”]/*[local-name()=’Employee’ and namespace-uri()=”])>2</XPathValidation>
  <!– Attribute id must equal ‘1’–>

  <XPathValidation query=/*[local-name()=’Employees’
and namespace-uri()=”]/*[local-name()=’Employee’ and namespace-uri()=”][1]/@*[local-name()=’id’
and namespace-uri()=”]>1</XPathValidation>
</XPathList>

Note: The count() XPath
function requires you to use XmlValidationStepEx. XmlValidationStep (as
at v2.2 of BizUnit) only supports XPath statements which result in a single node being
returned.

Back to Tech Blog