180 likes | 406 Views
Unit Testing BizTalk. An Intro to BizMock. Difficulties in Unit Testing BizTalk. The Value of Test-Driven Development is well-known. However, doing it in BizTalk can be difficult: SOA and ESB solutions are loosely-coupled by design and as a result, difficult to test.
E N D
Unit Testing BizTalk An Intro to BizMock
Difficulties in Unit Testing BizTalk The Value of Test-Driven Development is well-known. However, doing it in BizTalk can be difficult: • SOA and ESB solutions are loosely-coupled by design and as a result, difficult to test. • Testing can usually take place only after external solutions are available and online. • Most pieces of a BizTalk solution (such as pipelines, orchestrations, .dll’s) perform only narrow and specific tasks with many follow-on dependents. • Most BizTalk unit tests simply involve the act of copying a file to one location and picking output up from another.
BizMock as a Unit-Test Utility Available at http://bizmock.codeplex.com BizMock allows us to overcome many of the difficulties in unit-testing BizTalk by providing a framework to “mock-up” BizTalk endpoints. BizMock works with BizTalk as a special adapter that sends and receives messages into and from the BizTalk system Ports and other artifacts can be dynamically generated and removed at runtime. BizMock exposes a number of test functions which allow for richer BizTalk unit-testing than can be done in the typical “file drop” scenario.
BizMock Drawbacks The number one drawback of using BizMock is that it is very poorly documented. To understand how it works, you’ll need to spend a great deal of time studying BizMock’s source code, which is available in the CodePlex download.
Setting It Up • Deploy your BizTalk assemblies (orchestrations, pipelines, maps, schemas) • Create a test project and add BizMock assembly references: • BizMockArtifactsSchema • BizMockery • BizMockMessaging • BizMockReceiveAdapter • BizMockSamples • Add artifacts.tt and artifacts.xml from the Sample project to your test project • Modify artifacts.xml to reflect your artifact definitions • Execute the template to generate your code • Write unit tests
Artifacts.xml: Custom Types • Message Verifiers : Validates messages against schema and property. • Single Message Verifiers • Multipart Message Verifiers
Artifacts.xml: Custom Types (cont’d) • Message Instances • Single Message Instance • Multipart message Instance
Artifacts.xml: Artifacts • Artifacts element Attributes: • name: choose the name you wish your artifacts collection class to be called in code. For most purposes, the default value , “Artifacts”, is a pretty good choice. • appName: the name of the BizTalk application that artifacts will be deployed to for testing. • Message Instances <MessageInstance> <Name>Msg_Simple</Name> <Type>SimpleMessageType</Type> <Files> <File>SimpleSchema_input.xml</File> </Files> </MessageInstance> • Message Verifiers <MessageVerifier> <Name>Vrf_Simple</Name> <Type>SimpleSchemaTypeVerifier</Type> </MessageVerifier>
Artifacts.xml: Artifacts (cont’d) • Maps <Map> <Name>xyzMap</Name> <MapFullName>BizMock.Samples.xyzMap</MapFullName></Map> • Ports • Two-Way Send <TwoWaySendPort> <Name>GetDataService</Name> <PortName>GetDataService</PortName> <SendPipeline> <Name></Name> <Data></Data> </SendPipeline> <ReceivePipeline> <Name></Name> <Data></Data> </ReceivePipeline> <Dynamic>true</Dynamic></TwoWaySendPort>
Artifacts.xml: Artifacts (cont’d) • Ports • One Way Receive <OneWayReceiveLocation> <Name>OWPort_1</Name> <PortName>OWPort_1</PortName> <ReceivePipeline> <Name></Name> <Data></Data> </ReceivePipeline></OneWayReceiveLocation> • Databases <Database> <TypeName>DatabaseArtifact</TypeName> <Name></Name> <ConnectionString></ConnectionString></Database>
Artifacts.xml: Artifacts (cont’d) Event Log <EventLog> <Name>BizMock_EventLog</Name> <Log>Application</Log> <Source>BizMock</Source></EventLog> Orchestration <Orchestration> <Name>MultipartWithLoopOrchestration</Name> <FullName>BizMock.Samples.MultipartWithLoop</FullName> <Ports> <Port> <Logical>Port_1</Logical> <Physical>OWPort_1</Physical> </Port> <Port> <Logical>Port_2</Logical> <Physical>OWPort_2</Physical> </Port> </Ports></Orchestration>
Writing BizMock Unit Tests Add test documents to your project Call the Initialize() method of your artifacts class in the ClassInitialize() or TestInitialize() method of your testing class Call BizMockery.DeleteAllInstances() in your ClassCleanup() method. In each TestMethod, be sure to add a DeploymentItem for each test document used.
Sample Test [TestMethod][DeploymentItem("SimpleSchema_input.xml")][DeploymentItem("SimpleSchema2_input.xml")][DeploymentItem("Service1_request.xml")]publicvoidMultipartWithLoopTest(){artifacts.MultipartWithLoopOrchestration.Start();artifacts.Msg_MultiPart.InterchangeID = "X";//art.Vrf_Multipart.MultiPart1.Field = "Y"; //art.Vrf_Multipart.MultiPart2.Field = "Y";Submit.Request(artifacts.Msg_MultiPart).To( artifacts.OWPort_1);Expect.AtLeast(2).Request.At(artifacts.OWPort_2).Verify( artifacts.Vrf_Multipart);}
Contact Info • Ed Jones, MCT, MCPD, MCTS • Email: talentedmonkey@hotmail.com • Blog: Extremely Talented Monkeyshttp://talentedmonkeys.wordpress.com