Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

9. Testing BizTalk Artifacts > Unit testing a BizTalk solution with BizUnit

Unit testing a BizTalk solution with BizUnit

Unit testing a BizTalk solution with BizUnit BizUnit is a framework for testing BizTalk artifacts. It offers a flexible and extensible declarative test framework. The BizUnit framework has no dependency on Visual Studio unit testing. Yet, you can leverage the framework in the Visual Studio Test Project. A developer authors the BizUnit test case(s) in XML or code (BizUnit object model), and by doing so he/she configures how the test framework should execute the case(s). A test case is made up of three stages: Test setupTest executionTest cleanup Each stage in a test case can have zero or more test steps, and these test steps are, in general, autonomous. This means that the state can be flowed between each state if required by using the context object which is passed to each test step by the framework. Currently, BizUnit is at its fourth version supporting BizTalk 2010 (.NET 4.0) and this recipe will show how to use BizUnit 4.0 to unit test an orchestration in BizTalk 2010. The test case demonstrated in this recipe will execute a test case through code. Getting ready Download BizUnit from CodePlex (http://bizunit.codeplex.com/releases/view/66850). Install BizUnit on your machine. For reference, you can download the sample solution belonging to this chapter from the Packt website. How to do it... The following steps will show how to perform automated tests with BizUnit: Open the solution you want to test in Visual Studio 2010.Add a test project to the solution: Give the project an appropriate name.Click on OK.Next, you need to add the BizUnit references to the test project. You will find them at<install path>\BizUnit\BizUnit 4.0\Bins. Reference to the following assemblies needs to be added: BizUnit.dllBizUnit.TestSteps.dllBizUnit.TestSteps.BizTalk.dll Now you can create your test methods. Rename UnitTest1.cs to an appropriate name if you desire.Add the appropriate using statements to the test class.Create a test method and add the following code snippet: [TestMethod] public void TestEmployeeContractOrchestration() { var employeeContractTest = new TestCase { Name = "Test EmployeeContract Orchestration" }; //Delete of any files that are already there DeleteStep deleteStep = new DeleteStep(); var filePathsToDelete = new Collection<string> { @"C:\BizTalk Server 2010 Cookbook\Chapter 9\BTS.BizUnit.Sample\Out\*.xml" }; deleteStep.FilePathsToDelete = filePathsToDelete; employeeContractTest.SetupSteps.Add(deleteStep); //Create the test step var testStep = new CreateStep(); //Where are we going to create the file //Change directory to your own requirement testStep.CreationPath = @"C:\BizTalk Server 2010 Cookbook\Chapter 9\BTS.BizUnit.Sample\ In\EmployeeInstance.xml"; var dataLoader = new FileDataLoader(); //Where are we getting the file from //Change directory to your own requirement dataLoader.FilePath = @"C:\BizTalk Server 2010 Cookbook\Chapter 9\BTS.BizUnit.Sample\TestInstances\ EmployeeInstance.xml"; testStep.DataSource = dataLoader; employeeContractTest.ExecutionSteps.Add(testStep); //Create a validating read step //We should only have one file in the directory var validatingFileReadStep = new FileReadMultipleStep { DirectoryPath = @"C:\BizTalk Server 2010 Cookbook\Chapter 9\BTS.BizUnit.Sample\Out\", SearchPattern = "*.xml", ExpectedNumberOfFiles = 1, Timeout = 3000, DeleteFiles = true }; //Create an XML Validation step //This will check the result against the XSD for the Employee document BizTalk solutionsBizTalk solutionsunit testing, BizUnit usedvar validation = new XmlValidationStep(); var employeeSchema = new SchemaDefinition { XmlSchemaPath = @"C:\BizTalk Server 2010 Cookbook\Chapter 9\BTS.BizUnit.Sample\BTS.BizUnit.Sample.Schemas\ EmployeeContract.xsd", XmlSchemaNameSpace = "http://BTS.BizUnit.Sample.Schemas.EmployeeContract" }; validation.XmlSchemas.Add(employeeSchema); validatingFileReadStep.SubSteps.Add(validation); employeeContractTest.ExecutionSteps. Add(validatingFileReadStep); //Execute Tests var bizUnit = new BizUnit.BizUnit(employeeContractTest); bizUnit.RunTest(); } Build the test project.Test the project by clicking on Run Tests in Current Context (refer to the following screenshot) or Run all Tests in Solution: Check if the test has run successfully: How it works... A BizUnit test case in this recipe is created through code. First a TestCase is instantiated and then the first step is to delete any files in the location, where the bound port will send its messages to. This is necessary to bring the status to its original state, where no processing of messages has taken place. The first stage of the test case is to put it up-front, but you can choose to have this stage at the end. The next step is to define the test step(s) for the unit test to execute. The TestStep object has a CreationPath property that defines the path (directory and filename) that BizTalk should be watching with a Receive port. A FileLoader object is instantiated to enable to assign the FilePath property. This property contains the location to the XML message which is going to be offered to the orchestration. Now, this step is fully configured and added to the TestCase object: employeeContractTest.ExecutionSteps.Add(testStep) The following steps involve checking the execution of the orchestration. Does the file exist in the output directory (has the orchestration processed the incoming message and sent it to the out folder), and does the message in the output directory adhere to the schema (for example, EmployeeContract)? A FileReadMultiStep object is created and a few properties are set: The DirectoryPath property which defines the directory to readThe SearchPattern property for the type of fileExpectedNumberOfFiles to define the number of files to be expectedTimeOut to define the time (milliseconds) when timeout will occurThe DeleteFiles property to determine whether or not to delete the file To determine if the output message adheres to the schema a ValidationStep object and a SchemaDefinition object are instantiated. Then, for the next steps two more properties are set: XmlSchemaPath to determine the location of the schemaXmlSchemaNamespace to set the namespace (that is, targetNamespace) To the validation.XmlSchemas.Add method, the instance of the SchemaDefinition object is offered. The instance of the ValidationStep object is added to FileReadMultiStep as a substep. Finally, the instance of this object is added to an instance of the TestCase object (that is, employeeContractTest). Now the test setup stage is complete. To execute the test case a BizUnit object is instantiated with reference to the TestCase object (that is, employeeContractTest). Then the Run method is called. This concludes the test execution stage. As the complete test case is coded in a test class of a test project in Visual Studio, you can execute the test within Visual Studio and view the result. There's more... You will find more background information on BizUnit itself on MSDN and CodePlex: Using BizUnit for Automated Testing:http://msdn.microsoft.com/en-us/library/cc594538(v=BTS.10).aspxBizUnit - Framework for Automated Testing of Distributed Systems: http://bizunit.codeplex.com/ On CodeProject, you will find an article called BizUnit 4.0 and BizTalk 2010 (http://www.codeproject.com/KB/biztalk/BizUnit4BTS2010.aspx). This article explains about BizUnit 4.0 and BizTalk 2010 in more detail. See also Refer to the Testing BizTalk artifacts inside Visual Studio recipe discussed earlier in this chapter

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint