BizTalk Unit Testing Issue while validating instances against the Flat File Schemas

Consider the following scenario:
You are validating an instance against a Flat File Schema using the Validate Instance Option.

You are able to validate the schema successfully and get the validate Instance output as true.

Now if you are using BizTalk Unit Testing to validate the instance against the schema the following statement:

Assert.IsTrue(target.ValidateInstance(instanceFilePath, OutputInstanceType.Native));
might return False

This scenario will happen in case if you are using Imports while creating the Flat File Schema. 

 

This is a known issue with BizTalk Server 2010, that in case if we are trying to validate an instance against a Flat File Schema, which contains other imported schema then it would return False, even though the instance is valid. 
Here is a workaround that can be used for BizTalk Unit testing to validate such schema instances. 
You need to write the following code:

[TestClass]
    public class UnitTest
    {
        private TestContext testContextInstance;       

        ///Gets or sets the test context which provides 
        ///information about and functionality for the current test run. 

        public TestContext TestContext
        {
            get
            { return testContextInstance;

            }
            set
            {
                            testContextInstance = value;
            }
        } 

        [TestMethod]
        [TestCategory("Integration")] 

// Make sure that the schemas that are imported while creating a flat file schema are deployed first.
        [DeploymentItem(@"C:\\Import1.xsd")] 
        [DeploymentItem(@"C:\\Import2.xsd")]
        [DeploymentItem(@"C:\\Import2.xsd")]

public void TestMethod1()
        {
var target = new SchemaMethod();
string instanceFilePath = @"C:\Instance.txt";  

//Make sure to manually copy the schemas with full name space.      
File.Copy(@"C:\Import1.xsd" , testContextInstance.DeploymentDirectory + \\Import1);
File.Copy(@"C:\Import2.xsd" , testContextInstance.DeploymentDirectory + \\Import2);
File.Copy(@"C:\Import3.xsd", testContextInstance.DeploymentDirectory + \\Import3);           

bool check = target.ValidateInstance(instanceFilePath, OutputInstanceType.Native);
Assert.IsTrue(target.ValidateInstance(instanceFilePath, OutputInstanceType.Native));
}
}

You would now be able to validate the instances for Flat File Schemas having Imported Schemas.

Written by
Rasika Chaudhary

Reviewed by
Jainath V R 

Microsoft India GTSC