Changing the “object reference not set to an instance of an object” Exception in BizUnit 2.3 for Biztalk 2006

Lately I’ve been working with BizUnit 2.3 to create Unit tests for the Orchestrations we’ve build in BizTalk 2006 at our current customer.

BizUnit 2.3 is an opensource project on CodePlex (http://www.codeplex.com/bizunit ) to help you make the testing of these orchestrations easier. I’ve noticed that when you create a validation step and try to do a check with an xpath query you can get weird errors when the element you are looking for doesn’t exists.The Exception you’ll receive will be: 

 

object reference not set to an instance of an object

This Error doesn’t really say what is going wrong in your test report and that’s because the application isn’t throwing an ApplicationException but the code right in front of it will go wrong because checknode is null and BizUnit is trying to compare the checknode.InnerText.What I did was change the code in the XmlValidationStep.cs so it will give a nice ApplicationException when the element you are trying to check doesn’t exist in the xml file you are checking.The changes I made to the code are written down here and should be inserted into XmlValidationStep.cs in the BizUnit 2.3 project

 

string xpathExp = xpath.SelectSingleNode(“@query”).Value;
XmlNode valNode = xpath.SelectSingleNode(“.”);
string nodeValue = valNode.InnerText;
context.LogInfo(“XmlValidationStep evaluting XPath {0} equals \”{1}\”", xpathExp, nodeValue );
XmlNode checkNode = null;

try
{
  checkNode = doc.SelectSingleNode(xpathExp);
}
catch
{ }
if
(checkNode != null)
{

  if
(0 != nodeValue.CompareTo(checkNode.InnerText))
  {
    throw new ApplicationException(string.Format(“XmlValidationStep failed, compare {0} != {1}, xpath query used: {2}”, nodeValue, checkNode.InnerText, xpathExp));
  }
}
else

{
  throw new ApplicationException(string.Format(“XmlValidationStep failed, no element found, xpath query used: {0}”, xpathExp));
} 

Hope this will help you understand the errors you get while testing with BizUnit on your project! 

Geert van der Cruijsen

 

 

Share on Facebook
Kick It on DotNetKicks.com
Shout it
Post on Twitter