Home > Articles

  • Print
  • + Share This
This chapter is from the book

Testing of Maps

A map performs a transformation from one XML format into another. It is essential that the output generated is valid given the schema that describes the output, because otherwise you are sending invalid messages to trading partners and internal systems.

The Mapper helps you generate XSLT that generates valid XML, and it warns you about certain issues when validating the map or when compiling the project. Ultimately, however, it is the responsibility of the developer to make sure that the output generated by a map is valid.

This means that after developing your map you want to test it. Preferably, test all possible cases the map can get into at runtime. This section walks you through your options for testing your map.

Validating Maps

First of all, when developing a map, you should validate it. This is done by right-clicking the map file (.BTM) in Solution Explorer and choosing Validate Map. This will let Visual Studio 2010 go through the map and check for different kinds of syntactical errors such as functoids with the wrong number of inputs and other such things. If no errors occur, the map can be compiled and deployed.

The validation might be successful but with some warnings. If warnings occur, you must decide whether to ignore them because you know you have handled the issue the warning is about or whether you must do something about it. A warning that you will probably see many times is the “Warning btm1004: The destination node ‘NameOfNode’ has multiple inputs but none of its ancestors is connected to a looping functoid.” Basically, this warning comes because you have multiple source nodes connected to the same output node. This can be by design if you are using multiple Value Mapping functoids to do conditional mapping of values into one node.

Warnings are there for a reason, so take them seriously and deal with them all. As a general rule, BizTalk by default does not validate any messages sent out, meaning that your map really needs to be working well.

Testing Maps

After validating your map, you can test it from within Visual Studio 2010. To test the map, you need to provide Visual Studio 2010 with a test instance. You can set some properties on a map file (.BTM) in Solution Explorer to facilitate this. Figure 3.40 shows these, and they are explained in Table 3.15.

Figure 3.40

Figure 3.40. The properties you can set on a .BTM file in Solution Explorer.

Table 3.15. Properties on .BTM Files for Testing a Map



TestMap Input

Can be either Generate Instance, XML, or Native. If set to Generate Instance, Visual Studio 2010 generates an instance of the source schema for the map and uses that as test instance no matter what the other properties are set to. If set to XML, Visual Studio 2010 assumes that the instance you are providing for testing the map is in XML format. If set to Native, Visual Studio 2010 assumes that the instance you are providing is in the native format of the source schema. This allows you to use a flat file or EDI instance as a test instance. Visual Studio 2010 then first converts it into XML using the appropriate schema and editor extensions and uses the XML as input.

TestMap Input Instance

Full path to the file to use as test instance for the map. If the TestMap Input is set to Generate Instance, this property is ignored.

TestMap Output

Can be set to either XML or Native. Determines whether the output of testing the map should be in XML format or the format that is native for the destination schema in the map.

TestMap Output Instance

Full path to where Visual Studio 2010 should write the output of testing the map. If this is not specified, the output is written to a temporary file. The full path to the file that is generated is always written in the output window, giving you access to open it after the test is done.

Validate TestMap Input

If set to true, the instance that is either generated or read from a file is validated against the schema before the map is executed. If set to false, the map is executed without validation.

Validate TestMap Output

If set to true, the output of the map is validated against the schema for the output. If set to False, the output is not validated and written to a file as is.

After setting the properties as you want, you can test the map by right-clicking the .BTM file and choosing Test Map, as shown in Figure 3.41.

Figure 3.41

Figure 3.41. How to test your map.

After you choose the Test Map option, Visual Studio 2010 reads the properties on the .BTM file as specified in Table 3.15 and tests the map. If the test is successful, you get a link to the generated output in the output window. If the test fails, you receive a list of errors in the Error List window. A test is considered successful if no exceptions are thrown during execution and if input and output validation succeeds, if turned on. Exceptions can occur during execution if the Assert functoid is used or if a functoid actively throws an exception.

Debugging a Map

If your map does not provide you with the output you need and expect, some debugging might be in order. BizTalk supplies you with the option to debug your map line for line to see what happens.

Unfortunately, this functionality does not work for referenced functoids because the debugger cannot find the right external assemblies at runtime. If you want to debug your map and you are using functoids, it is therefore a good idea to make sure you are using the inline versions of all functoids, where possible. The functoids that are shipped with BizTalk, for instance, often have both an inline implementation and a referenced implementation. Which implementation to use when a functoid supports multiple implementations is controlled by the Script Type Precedence property of the Mapper grid. When you click the ellipsis for that property, you get a small window where you can set the script type precedence, as shown in Figure 3.42.

Figure 3.42

Figure 3.42. Setting the Script Type Precedence property.

Basically, you should get the External Assembly possibility moved to the bottom to make sure external assemblies are used as a last resort. If you trust that the inline versions of the functoids is equal to the version that is referenced, you can change the order back after debugging if you want to.

After setting the Script Type Precedence, you need to set the value of the TestMap Input Instance property to point to the instance you want to use as input when debugging the map. You can also specify the TestMap Output Instance if you would like to control the filename the output is written to.

After setting these properties, you can right-click your .BTM file in Solution Explorer and choose Debug Map. Visual Studio 2010 then generates the XSLT that is to be debugged and opens it with a breakpoint already set on the first line. Besides this pane, two other panes are also opened. The first contains the output, which lets you keep track of the output that is built while the map is being debugged, and the other is the input XML, which lets you see which fields in the input XML are currently used to build the output. You can drag the windows around so that you can see all three panes at the same time. Figure 3.43 illustrates this.

Figure 3.43

Figure 3.43. Debugging a map.

As you can see in Figure 3.43, three panes are open: the XSLT, the input XML, and the generated output. As you can also see at the bottom of the figure, you get the watches, as well, thus enabling you to keep track of the values of the variables and optionally change the values at debug time to test what will happen. Also, you can set breakpoints and use F5 (to run until next breakpoint), F10 (to step over the currently active line of code), and F11 (to step into the currently active line of code), as you are used to doing when debugging .NET code inside Visual Studio 2010.

Unit Testing

After a map is developed, validated, tested, and possibly debugged, you should enable unit testing of the map. As a matter of fact, many great people insist you should write your unit tests before even starting developing anything. This is called test-driven development (TDD). In either case, BizTalk ships with the option to do unit testing on your maps, which you should leverage, and this section describes this functionality.

The first thing to do is to enable unit testing on the project that contains a map you want to unit test. To do so, follow these steps:

  1. Go to Solution Explorer.
  2. Right-click the project that contains the map that is to be unit tested, and choose Properties.
  3. Go to the Deployment pane and enable the Enable Unit Testing property, as shown in Figure 3.44.
Figure 3.44

Figure 3.44. Enabling unit testing on project.

After enabling unit testing on the project, all maps in the project that are compiled will be inheriting from the TestableMapBase class in the Microsoft.BizTalk.TestTools.Mapper namespace instead of the normal TransformBase class in the Microsoft.XLANGs.BaseTypes namespace. The TestableMapBase class actually inherits from TransformBase, so nothing is lost. What is gained, however, are some methods and properties that can be leveraged for unit testing.

Next, you should add a test project to your solution. This is done by using the menu in Visual Studio 2010, where you can click Test, New Test to open the screen shown in Figure 3.45. In this screen, you can choose to either add the new test to an existing test project, if one is present in the solution, or create a new Visual C# test project. In addition, you can choose between four different tests:

  • Ordered Test: This gives you a way of orchestrating your tests, deciding what order they should be performed in.
  • Unit Test: This option gives you a class to write your tests in, but some manual work needs to be done like referencing the right assemblies and so on.
  • Basic Unit Test: This option provides an even smaller and simpler version of a unit test class than the unit test. More to implement yourself.
  • Unit Test Wizard: This helps you through some of the choices you must make, like what maps to test, and then generates the test class for you.

The other five options for adding a new test are not relevant for a BizTalk project.

For your first test project, name your file, choose Unit Test, and click OK. If you already have a test project, you can decide to add the file to an existing project by selecting it in the drop-down.

The test project is created for you, and it includes a pretty empty class for your tests. The class contains some definitions, methods, a constructor, and one method that is really the one thing to focus on, because this is the one you need to implement.

Figure 3.45

Figure 3.45. Adding a new test to your test project.

So, what you need to do in this method is to implement the unit test of the maps you want to test in this test class. Remember that you can have as many test classes and test methods in each test project as you want.

To test a map, you need to reference three different assemblies from your test project:

  • The assembly that contains the map: This assembly must have the Enable Unit Testing property set to True.
  • The Microsoft.BizTalk.TestTools assembly: This is found in the .NET pane of the Add Reference screen.
  • The Microsoft XLANG/s Base Types assembly: This is also found in the .NET pane of the Add Reference screen.

In the project, the XML instances that are to be used for the unit test can also be added. If you do this, consider marking their Build Action property to None, so they are not compiled needlessly into the assembly. The instances are not required to be included in the project, but it gives you a nice way of having everything needed for the test grouped together.

After adding the project references, you need to implement the test method. Locate the method in the class file called TestMethod1, and change it to something like the code shown in Listing 3.20.

Listing 3.20. Sample Test Method for Testing a Map

public void TestMapC1702OrderToFineFoodsOrder()
    string INPUT = testContextInstance.TestDir + @"\..\Order.xml";
    string OUTPUT = testContextInstance.TestDir + @"\..\MappedOrder.xml";
    TestableMapBase map = new Order_to_InternalOrder();
    map.ValidateInput = true;
    map.ValidateOutput = true;
    map.TestMap(INPUT, InputInstanceType.Xml,
        OUTPUT, OutputInstanceType.XML);
    Assert.IsTrue(File.Exists(OUTPUT), "File does not exist");
    // Read in OUTPUT and check relevant values.
    // Compare file with expected output.

The two strings INPUT and OUTPUT are declared to contain the path to the input instance for the map and the path to the output file to write the output to. The functionality required basically instantiates the map as a TestableMapBase class, which contains the needed properties and methods for unit testing. Then the properties ValidateInput and ValidateOutput are set to true. These properties mean the same as the properties you can set on the .BTM file and will determine whether the TestMap method should validate the input and output against the respective schemas before and after the map is executed. Any failures in this validation results in the test failing. Both values are false by default.

For the code to compile, the using statements shown in Listing 3.21 are needed. The namespaces are as follows:

  • Microsoft.VisualStudio.TestTools.UnitTesting: This is the namespace needed to use the TestClass and TestMethod attributes.
  • FineFoods.Customers.FamilyRestaurant: This is the namespace the map is located in, providing you access to the Order_to_InternalOrder class that is the compiled map.
  • Microsoft.BizTalk.TestTools.Mapper: This namespace contains the TestableMapBase class needed to call methods on the map object.
  • Microsoft.BizTalk.TestTools.Schema: This namespace contains the two enumerations used for specifying the type of input and the type of the output for the TestMap method.
  • System.IO: This is used to be able to access the File class that is used to check whether the output file exists in the assertion that is in the test method.

Listing 3.21. using Statements Necessary for Code in Listing 3.20

using Microsoft.VisualStudio.TestTools.UnitTesting;
using FineFoods.Customers.C1702;
using Microsoft.BizTalk.TestTools.Mapper;
using System.IO;

using Microsoft.BizTalk.TestTools.Schema;

The code in Listing 3.20 shows just one assertion, which basically asserts that the output file is created. You might need other assertions, such as comparing the output to an instance of the expected output or reading in the output file and validating some of the actual values created.

You should have as many instances as needed to make sure your map can handle all possible instances correctly. This means you should have plenty of input instances and possibly plenty of samples of the output.

You can find Microsoft’s description of the unit test features at http://msdn.microsoft.com/en-us/library/dd224279(BTS.70).aspx.

  • + Share This
  • 🔖 Save To Your Account