Testing BizTalk XLANGMessage with the Fakes Framework

Ive been interested in exploring the opportunities around the Fakes Framework for testing BizTalk solutions for a while now, and until recently its been sitting as one of the many blog articles I have intended to write but haven't got around to. This week we have started looking at a BizTalk 2013 project and this presented an excellent opportunity to explore this idea.

I decided to look at the common problem people have faced in the past when you want to unit test some code and it has a dependency on the underlying BizTalk assemblies and they were difficult to mock out with one of the common mock frameworks. One of the most common examples was when you write a helper class for an orchestration and the method takes an XLANGMessage object as an input parameter. This is quite a common pattern that people have struggles with. Often the way people would handle this was to write a method that takes XLANGMessage and in that method extract the message body then call a 2nd method which did the logic required. This approach meant you could easily unit test the 2nd method and the first method people would often skip unit testing.

Although this wasn't ideal, it often worked and we would just accept this method would be tested as part of the BizUnit tests of the entire solution.

Let's take a look at a sample method that we may be trying to test.

!(/images/michaelstephenson/152824/c0065eae-043013_0018_TestingBizT1.png)

You can see that we want to extract the message body from the BizTalk message and then return it as a string. The big problem with unit testing this method is the dependency on the XLANGMessage object and how difficult it could be to correctly construct this object to pass into the test.

Since we now have Visual Studio 2012 available this brings in the Fakes Framework and we have a few new things available to us which will make this easier. First we need to look at our references to a couple of the underlying BizTalk assemblies and create Fakes for them. To do this right click on the references assembly and click Add Fake Assembly

!(/images/michaelstephenson/152824/2f3a030f-043013_0018_TestingBizT2.png)

Once we have a Fakes assembly we can then create Shims and Stubs of the various objects which are within this assembly. Im not going to go too much into the detail behind shims and stubs, but will provide some links to reference articles later.

We can now write a unit test where we can fake the reference to the underlying BizTalk assemblies and make them much easier to work with and allow us to focus on the method we want to test. The below code snippet shows how we will create our test. It will call a helper method which will create the shim of the XLANGMessage and then pass it into our method to test.

!(/images/michaelstephenson/152824/983c0fa7-043013_0018_TestingBizT3.png)

You can see in the above method that our test becomes very simple while still using the XLANGMessage or BTXMessage which derives from it. One key point to note in this method is the use of the ShimContext and its disposal which ensure the shim framework is created and cleaned up after our tests have used it.

Now lets look at the method which creates our message.

!(/images/michaelstephenson/152824/9e863cd3-043013_0018_TestingBizT4.png)

You can see this method is where the fakes framework magic is happening. We firstly create a stub of the XLANGPart object and assign our string to be its stream that is returned when we call the RetrieveAsType method. We then need to create stubs of a couple of other classes which are needed to create the stub of the BTXMessage. On this message we set the return for the intexer property to be our XLANGPart which we created earlier.

Finally we create a shim of the BTSMessage and return it.

This method for creating a fake XLANGMessage is probably quite reusable for most of your unit testing scenarios as we tend to usually interact with the message body in a helper class but not that much else.

Conclusion

Hopefully this example has shown you how easy it can be to use the Fakes framework to test code which you may have previously thought was untestable. This happens a lot when you have dependancies on the underlying BizTalk assemblies in your unit tests.

I think there could be quite a few other opportunities where this framework could be used to complement our existing BizTalk testing practices and I plan to explore these over the coming weeks.

The code for this sample is available on the following link:

References

I mentioned earlier Id point out a few other sources if your interested in the fakes framework and want to find out more. Id probably start with the following:

This article is part of the GWB Archives. Original Author: Michael Stephenson

New on Geeks with Blogs