This project is read-only.

Create a Synchronous Plugin Integration Test

In this example, we explain how to test the plugin "PostContactCreate". This synchronous plugin creates an Identity Check record every time a contact is created, therefore this record can be used by other process later on. So, our test is going to verify that only one Identity Check record is created with the right information.

To create this test, just follow the next steps:
  • Right click on the folder Plugins of the Integration Test project
  • Add new Item
  • Dynamics CRM => "Plugin Integration Test"



PostContactCreate.cs code:
public class PostContactCreate: Plugin
        public PostContactCreate()
            : base(typeof(PostContactCreate))
            base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Create", "contact", new Action<LocalPluginContext>(ExecutePostContactCreate)));

        protected void ExecutePostContactCreate(LocalPluginContext localContext)
            Contact contact = GetContactFromContext(localContext);
            localContext.TracingService.Trace("Creating service context");
            using (XrmServiceContext context = new XrmServiceContext(localContext.OrganizationService))
                xdft_identitycheck newIdentityCheck = new xdft_identitycheck();
                newIdentityCheck.xdft_ContactId = contact.ToEntityReference();
                newIdentityCheck.xdft_FirstName = contact.FirstName;
                newIdentityCheck.xdft_LastName = contact.LastName;
                if (contact.BirthDate != null)
                    newIdentityCheck.xdft_DateOfBirth = contact.BirthDate.Value;

        private Contact GetContactFromContext(LocalPluginContext localContext)
            if (localContext == null)
                throw new ArgumentNullException("localContext");

            localContext.TracingService.Trace("Getting new contact from context");
            if (localContext.PluginExecutionContext.InputParameters.Contains("Target") &&
            localContext.PluginExecutionContext.InputParameters["Target"] is Entity)
                return ((Entity)localContext.PluginExecutionContext.InputParameters["Target"]).ToEntity<Contact>();
                throw new InvalidPluginExecutionException("Contact record was not found as Target in Plugin Context");

TestPostContactCreate.cs code:
public class TestPostContactCreate : PluginIntegrationTest
        #region Instance Variables

        private Contact contact;

        private xdft_identitycheck identityCheck;


        #region Setup

        protected override OrganizationRequest SetupTriggerRequest()
            contact = new Contact();
            contact.FirstName = "James";
            contact.LastName = "Bond";
            contact.BirthDate = DateTime.Now;

            CreateRequest request = new CreateRequest();
            request.Target = contact;
            return request;


        #region Test

        public void RunTestPostContactCreate()


        #region Verify

        protected override void Verify()

            CreateResponse response = this.TriggerResponse as CreateResponse;
            contact.Id =;

            using (XrmServiceContext context = new XrmServiceContext(this.OrganizationService))
                var identityChecks = from ic in context.xdft_identitycheckSet
                                     where ic.xdft_ContactId.Id == contact.Id
                                     select ic;

                Assert.IsTrue(identityChecks.ToList().Count() == 1);

                identityCheck = identityChecks.First();
                Assert.AreEqual(identityCheck.xdft_FirstName, contact.FirstName);
                Assert.AreEqual(identityCheck.xdft_LastName, contact.LastName);
                Assert.AreEqual(identityCheck.xdft_DateOfBirth.Value.Date, contact.BirthDate.Value.Date);


        #region Clean Up

        protected override void CleanUp()
            this.OrganizationService.Delete(identityCheck.LogicalName, identityCheck.Id);
            this.OrganizationService.Delete(contact.LogicalName, contact.Id);


As you can see, the test is clearly composed by four sections:
  • Setup: Create and initialised the required test records. In this case, prepare the contact record to be created.
  • Test: Execute the corresponding operation/message, such as CreateRequest in this example
  • Verify: It is the heart of the test where the assertion are evaluated. In this case, we verify that only one "Identity Check" record is created with the right information.
You should specially notice the assertion Assert.IsNull(Error), which is offered out of the box with the xRM Test Framework to verify base failures or exceptions (e.g. Service not available or authentication error)
  • Clean Up: It tidies up again the environment after the Setup and Test phases. For instance, in this test we need to delete the created Contact and Identity Check records.

Last edited Jun 9, 2014 at 7:28 PM by rtebar, version 5


No comments yet.