Biztalk maps and unit tests

Florian Cailalud
Published by Florian
Category : BizTalk
01/06/2018

This article follows the presentation of unit testing on XML schemas. This time we will focus on the unit tests for BizTalk maps. The philosophy remains the same: to guarantee the validity and the non-regression of transformations throughout a project.

Once again, we’re going to create a simple BizTalk 2016 project with two XML schemas and a map allowing the transformation from the first to the second. For information, the Script fonctoïd allows to make a « complex » concatenation as we will see next.

map unit test biztalk project

 

Then you need to reach the BizTalk project properties to activate « Enable Unit Testing » option.

enabling unit test BizTalk map

This action will make the maps inherit from Microsoft. BizTalk. TestTools.Mapper.TestableMapBase class, itself inheriting from Microsoft.XLANGs.BaseTypes.TransformBase (So we keep all BizTalk maps properties).

It is possible to check the inheritance change by opening the C# file associated with the map.

One limitation of this approach is that the Microsoft.Biztalk.TestTools assembly is not natively present on the BizTalk Server production version. However, there are two solutions :

* Either add this assembly to your production environment

* Either define a Debug profile (with « Enable Unit Testing » option to « True »), and a Release profile (with the option to « False »)

The second Solution still, in my opinion, the cleaner.

Similarly to the XML schemas unit tests, we will then propose two different methods for testing a map within a unit test project.

The first method tests the execution of the map by providing the address of the source file and the address of the output file (with their respective XML or native type). It is also possible to choose the type “generate” to generate the source.


[TestMethod]

public void Validate_Address1_To_Address2_FirstMethod()
{

  MapUnitTesting.Address1_To_Address2 map = new MapUnitTesting.Address1_To_Address2();
  map.ValidateInput = true;
  map.ValidateOutput = true;

  string strSourceSchema = @"..\..\..\Sample\Address1_example.xml";
  string strDestinationSchema = @"..\..\..\Sample\Address1_example_out.xml";



  map.TestMap(strSourceSchema,
  InputInstanceType.Xml,
  strDestinationSchema,
  OutputInstanceType.XML);
  Assert.IsTrue(File.Exists(strDestinationSchema));

}

This approach implies certain limitations:

  • For each case related to the map, the corresponding source file must be added.
  • Even if it is possible to validate the source and the destination, the data verification of the output file will be at your expense.

The second method uses the Map Test Framework (MTF), which is located (here). This Framework is quite simple to use. Indeed, copy the download folder, unzipped, to the desired location and then add a reference, in the unit test project, to the MapTestFramework.Common assembly. The Framework’s documentation can be find on the site.

In a very straightforward way, we need to inherit our test class from the MappingFixture class. In this context, the ExpectedPathBase, SourcePathBase, and CreateMap methods must be implemented (as shown below). It is, then, easy to test much more finely our map.

        protected override string ExpectedPathBase
        {
            get
            {
                return @"..\..\..\Sample\Address1_example_out.xml";
            }
        }

        protected override string SourcePathBase
        {
            get
            {
                return @"..\..\..\Sample\Address1_example.xml";
            }
        }

        protected override IMapExecuter CreateMap()
        {
            return new TestableMapBaseMapExecuter(new Address1_To_Address2());
        }

        [TestMethod]
        public void Validate_Address1_To_Address2_SecondMethod_BaseTest()
        {
            base.ExecuteBaseTest();
        }

        [TestMethod]
        public void Validate_Address1_To_Address2_SecondMethod_AdditionalTest_Sex()
        {
            string[] inputs = new string[]  {
                                                "/*[local-name()='Address1' and namespace-uri()='http://MapUnitTesting']/*[local-name()='Person' and namespace-uri()='']/*[local-name()='Sex' and namespace-uri()='']",
                                                "/*[local-name()='Address1' and namespace-uri()='http://MapUnitTesting']/*[local-name()='Person' and namespace-uri()='']/*[local-name()='FirstName' and namespace-uri()='']",
                                                "/*[local-name()='Address1' and namespace-uri()='http://MapUnitTesting']/*[local-name()='Person' and namespace-uri()='']/*[local-name()='LastName' and namespace-uri()='']"
                                            };
            string[] outputs = new string[] { "/*[local-name()='Address2' and namespace-uri()='http://MapUnitTesting']/*[local-name()='Name' and namespace-uri()='']" };

            MapTestCases collection = new MapTestCases(inputs,outputs);

            collection.AddTestCase(
                new string[] { "Male", "Florian", "CAILLAUD" }, new string[] { "Mr Florian CAILLAUD" }
                );

            collection.AddTestCase(
                new string[] { "Female", "Hao", "WU" }, new string[] { "Mme Hao WU" }
                );

            base.ExecuteMapTest(collection);
        }

In this case, we provide, through the ExpectedPathBase and SourcePathBase methods, templates of the source and destination files.
It is then possible, using the ExecuteBaseTest method, to quickly test (as for the first method) our map.
But, as the Validate_Address1_To_Address2_SecondMethod_AdditionalTest_Sex test shows, thanks to the XPath paths, certain fields can be configurable. Thus, the Framework allows you to give a list of parameters in input and a list of the expected values in output.
One can then test the “complex” concatenation of the map, which aims at bringing together, within the same field, title (based on sex), first name and name of a person.
This Framework brings other features, which are not detailed in this article, that allows you to set up a whole range of unit tests around BizTalk maps.

These two types of unit tests provide you with a simple and fairly effective way to ensure the validity and non-regression of your Biztalk transformations.