Unit Testing In Biztalk 2009
Introduction:
BizTalk Server 2009 is the latest major release of BizTalk Server. BizTalk 2009 has, for the first time, built-in developer support for testing schemas, maps and pipelines that is available for automated unit testing rather than being hidden behind features in Visual Studio where it can only be used for manual testing.
BizTalk Server 2009 introduced a unit testing feature that can be enabled on the Deployment property page of a BizTalk project. The following screenshot shows this project setting accessed from Project Designer when we right-click a project and click the Properties.
Screenshot of the Deployment tab in Project Designer exposing the Enable Unit Testing project property.
This feature allows us to create unit tests for schemas, maps, and pipelines.
When this feature is enabled and the project rebuilt, the artifact classes will be derived from the following base classes to support unit testing.
Artifact type | Base class |
Schema | Microsoft.BizTalk.TestTools.Schema.TestableSchemaBase |
Map | Microsoft.BizTalk.TestTools.Mapper.TestableMapBase |
Pipeline | Microsoft.BizTalk.TestTools.Pipeline.TestablePipelineBase |
Using the Unit Testing Feature with Schemas
This topic demonstrates how to use the unit testing feature to add a unit test for the schemas.
Testing Schemas
Schemas can be unit tested by using them to validate instance messages. To create Unit Test, we need to select Test->New Test from the Visual Studio Menu. This will bring up a dialog box where we will select Unit Test Wizard.
We have to enter a project name for the unit testing project when we are prompted for it.
After the Create button is pressed, we can see “Create Unit Tests” dialog box. We have to choose the Filter drop down list and select “Display Base Types”. Then we need to expand the Schemas Project, Schemas.SimpleRequest, Bases & Interfaces and select “Microsoft.BizTalk.TestTools.Schemas.TestableSchemaBase”.
Pressing OK button will create unit tests for each selected schema in the schemas in the project. It will also set all the necessary project references. We can validate XML as well as flat files.
The following code will be generated for the Test Method. We will need to set references to an input instance filename to validate the test instance.
[TestMethod()]
public void ValidateInstanceTestWithValidMessage()
{
TestableSchemaBase target = new SimpleRequest();
string inputInstanceFilename = @" ValidSimpleRequest.xml";
OutputInstanceType inputType = new OutputInstanceType();
inputType = OutputInstanceType.XML;
bool expected = true;
bool actual;
actual = target.ValidateInstance(inputInstanceFilename, inputType);
Assert.AreEqual(expected, actual);
}
The Solution Items file LocalTestRun.testrunconfig is to be opened. We need to select the Deployment screen and add the directory that contains all the test data and schemas that will be used during unit testing. This will allow us to use relative paths in our unit tests
Using the Unit Testing Feature with Maps
Maps can be tested by passing a test input instance to the map and validating that output message has been correctly transformed. We have to follow the exact same steps to generate a Unit Test for Map as it is for creating Unit Test for a Schema. In this case we have to select the class “Microsoft.BizTalk.TestTools.Mapper.TestableMapBase”.
But Multi-Input maps cannot be unit tested in this version of BizTalk.
The generated Unit Test will have a method where we just need to update information about the input and output message instances.
[TestMethod()]
public void TestMapAddTest()
{
TestableMapBase target = new SimpleRequest_To_SimpleResponse();
string inputInstanceFilename = @" AddSimpleRequest.xml";
InputInstanceType inputType = new InputInstanceType();
inputType = InputInstanceType.Xml;
string outputInstanceFilename = @"AddSimpleResponse.xml";
OutputInstanceType outputType = new OutputInstanceType();
outputType = OutputInstanceType.XML;
target.TestMap(inputInstanceFilename,
inputType, outputInstanceFilename, outputType);
XmlDocument output = new XmlDocument();
output.Load(outputInstanceFilename);
Assert.AreEqual("20", output.SelectSingleNode("/*[local-name()='SimpleResponse' and namespace-uri()='http://Schemas.SimpleResponse']/*[local-name()='Result' and namespace-uri()='http://Schemas.SimpleResponse']").InnerText);
}
Using the Unit Testing Feature with Pipelines
Both send and receive pipelines can be unit tested. Pipelines can be tested by passing a test input instance and schema to the pipeline. We have to follow the exact same steps to generate a Unit Test for Pipeline as it is done for creating Unit Test for a Schema. In this case we have to select “Microsoft.BizTalk.TestTools.Mapper.TestableReceivePipeline” for unit testing a receive pipeline and “Microsoft.BizTalk.TestTools.Mapper.TestableSendPipeline” for unit testing a send pipeline.
We have to customize the generated unit testing code with additional details about test instance messages and schemas.
Unit Test code to test receive and send pipeline is shown below.
[TestMethod()]
public void TestReceivePipelineTest()
{
TestableReceivePipeline target = new FFRcvSampleFile();
StringCollection documents = new StringCollection();
documents.Add(@"Sampe ASCII.txt");
StringCollection parts = new StringCollection();
Dictionary<string, string> schemas = new Dictionary<string,string>();
schemas.Add("Schemas.SampleSchema", @"SampleSchema.xsd");
target.TestPipeline(documents, parts, schemas);
}
[TestMethod()]
public void TestSendPipelineTest()
{
TestableSendPipeline target = new FFSendSampleFile();
StringCollection documents = new StringCollection();
documents.Add(@"SampleSchema.xml");
StringCollection parts = new StringCollection();
Dictionary<string, string> schemas = new Dictionary<string,string>();
schemas.Add("Schemas.SampleSchema", @"SampleSchema.xsd");
target.TestPipeline(documents, parts, schemas);
}
Known Issues
· The Unit testing feature for maps currently does not support multiple map inputs.
· When we are testing maps, output Instance file name cannot be null or empty.
· BizTalk SDK needs to be deployed to the machine running the pipeline unit tests.
Conclusion
Unit testing is a best practice when it comes to application development.
Until the release of BizTalk 2009 we had to rely on unit testing frameworks like BizUnit and Pipeline Testing Library to test BizTalk components. Having native support for testing Schemas, Maps and Pipelines makes unit testing easier.
By driving development with automated tests any developer can write reliable, bug-free code no matter what its level of complexity.
Comments
Post a Comment