Mocking up XLANGMessage and XLANGPart to unit test BizTalk components

BizTalk developers have often felt the need to unit test any dotnet components called from within an orchestration. However, messages passed to dotnet components are of type XLANGMessage and this class is an abstract class, therefore rendered useless when it comes to initialising these types in your unit tests. The way around this is to mock both the XLANGMessage and XLANGPart classes.

The best way to mock these classes is to run reflector on the xlang base types dll and look at the definition of these classes.

As you can see, both these classes are abstract classes and can be inherited and the behavior added. Below is the code the these mock classes. The key here is the constructor of these classes which will help us use them like any other class.


using System;
using Microsoft.XLANGs.BaseTypes;
using System.Xml.Schema;

namespace IntegrationExperts.BusinessObjects.Tests.MockClasses
{

public class MockXLANGPart : XLANGPart
{

readonly T _obj;

#region ctor

public MockXLANGPart(T obj) { _obj = obj; }

#endregion

#region props

public override string Name
{
get { return "MockXLANGPart"; }
}

public override XmlSchema XmlSchema
{
get { throw new NotImplementedException(); }
}

public override XmlSchemaCollection XmlSchemaCollection
{
get { throw new NotImplementedException(); }
}

#endregion

#region funcs

public override object GetPartProperty(Type propType)
{
throw new NotImplementedException();
}

public override Type GetPartType()
{
throw new NotImplementedException();
}

public override string GetXPathValue(string xpath)
{
throw new NotImplementedException();
}

public override void LoadFrom(object source)
{
throw new NotImplementedException();
}

public override void PrefetchXPathValue(string xpath)
{
throw new NotImplementedException();
}

public override object RetrieveAs(Type t)
{
if (t == typeof(T))
{
return _obj;
}
return null;
}

public override void SetPartProperty(Type propType, object value)
{
throw new NotImplementedException();
}

#endregion

#region dispose

public override void Dispose() { }

#endregion

}

}

//----------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Generic;
using Microsoft.XLANGs.BaseTypes;

namespace IntegrationExperts.BusinessObjects.Tests.MockClasses
{

public class MockXLANGMessage : XLANGMessage
{

readonly List<MockXLANGPart> _parts = new List<MockXLANGPart>();

#region ctor

public MockXLANGMessage(T obj)
{
_parts.Add(new MockXLANGPart(obj));
}

#endregion

#region props

public override int Count { get { return _parts.Count; } }

public override string Name
{
get { return "MockXLANGMessage"; }
}

public override XLANGPart this[int partIndex]
{
get { return _parts[partIndex]; }
}

public override XLANGPart this[string partName] { get { return _parts[0]; } }

#endregion

#region funcs
public override void AddPart(XLANGPart part)
{
throw new NotImplementedException();
}

public override void AddPart(object part, string partName)
{
throw new NotImplementedException();
}

public override void AddPart(XLANGPart part, string partName)
{
throw new NotImplementedException();
}

public override object GetPropertyValue(Type propType)
{
throw new NotImplementedException();
}

public override void SetPropertyValue(Type propType, object value)
{
throw new NotImplementedException();
}

public override IEnumerator GetEnumerator()
{
return _parts.GetEnumerator();
}

#endregion

#region dispose

public override void Dispose() { }

#endregion

}

}

Add these classes to your unit test project and in the test class, you can write tests as shown below:


[TestMethod]
public void DoActionTest()
{

   var msg = new MySchemaClass(); //schema class can be generated via tools like XSD to code.
   var xlang = new MockXLANGMessage(msg); // Create a new mock XLANGMessage to wrap the message
   DoAction(xlang); // Call the method to be tested by passing in the mock XLANGMessage
   //Assert.IsTrue(....);
}
internal static void DoAction(XLANGMessage message)
{

   var myObj = new MySchemaClass();//schema class can be generated via tools like XSD to code.
   myObj = (MySchemaClass)message[0].RetrieveAs(MySchemaClass.GetType()); //retrieve parts of the message.

   //var val1 = myObj.prop1;
   //var val2 = myObj.prop2;
   //perform additional tasks as per your testing requirements.
}

Advertisements

Troubleshooting SQL Azure connectivity

Below is very good compilation of tips to troubleshoot sql azure connectivity:
http://www.microsoftblog.co.in/?cat=414