Unit Testing Dynamics 365 plugin code

Unit Testing is crutial in software development which helps you proofing your development efforts and delivery in regards to desired functionality.

I'm not going to explain step by step how to create a Unit Test project in Visual Studio but focus on some remarkable topics.

In Dynamics 365 plugin development I have been using Moq and Microsoft.VisualStudio.TestTools.UnitTesting namespaces. Let's assume you have already created Test project in Visual Studio. Since you're going to use Moq need to install. Navigate to Nuget Package Manager console and install:

PM> Install-Package Moq

In your test class add a namespace :
using Moq;

Ensure your class is public and decorated properly:
[TestClass]

for instance:
[TestClass] public class TestAddContactToMarketingList

 

Implement your TestMethod and setup OrganizationService. Ensure you setup OrganizationService and call your business logic method, i.e: AddContactToMarketingList: