2011 m. spalio 18 d., antradienis

Simple UnitTest for BizTalk Map

Unit testing on BizTalk applications is pretty much hard task. All artifacts are different, and not easily executed from other code. Within this post I will introduce you to possibility to test BizTalk maps. In most cases there are a lot of problems if you want to test the connectivity, or the integration with another system. You have to count on your generated testing materials, which usually are not the same as found on the production.
There is one more question about map testing - is it usefull? I believe - YES, but not allways. When you reach some level of complexity and there can be at least some cahanges within map - it is better to have unit tests prepared. For small and simple maps it is unnecessary work. But for big ones, especially with a lot of nested loops - you should have some ways to check what your cahnges are effecting.
BizTalk 2010 already has some generators for unit test. You have to open your solution, then go to Visual Studio 2010 top meniu -> New Test...

Select "Unit Test Wizard" and you can choose either to create new test project or to add test files to the existing ones (using "Add to Test Project" drop down meniu).
In the "Create Unit Tests" dialog you should choose what classes and what method check you want to add to your unit test. In this case I am checking only map class, and press OK.
After pressing OK, wizard will automatically generate new project inside your solution, add all the required references and generate simple unit test cases for selected classes:
As we see the main line in all tests si map object initialization:

Map1 target = new Map1();

So, the target variable will hold our map, and it can be used to transform required input and output xml files. Here is some code from other map unit test, which is counting few tags from the new document and makes the decision if the map works correctly.

XDocument inputXml = TestingHelperComponents.OpenXmlFile("Transactions.xml");
StringWriter writer = new StringWriter();
 XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(inputXml.ToString());

 target.Transform.Transform(xmlDocument, null, writer);

XDocument outputXml = XDocument.Parse(writer.ToString());

string temp = string.Empty;

int inCount = inputXml.Descendants("Transactions").Count();
int outCount = outputXml.Descendants("Transactions").Count();

Assert.IsTrue(inCount > 0 && outCount == 0, "Transactions tag count: inCount: " + inCount + " OutCount: " + outCount);

As you see, I am checking only some tag occourence after transformation. This maybe more tedious task, than simple string comparison, but I believe it is more stable - after making some minor changes inside map, there can be cahnges inside the output, which have no effect on the result (from information point of view).

I believe it would be good to make xml comparison method, which could compare inner xml data, regardless to formating, whitespaces, empty or null tags.

In the future I hope to try BizUnit or NUnit for testing BizTalk Solutions.

Komentarų nėra:

Rašyti komentarą