I'm developing a new C# 3.5 app that needs to monitor an Exchange mailbox and perform some operations when emails are received. I'm aware that Microsoft now recommends using Exchange Web Services to perform operations on Exchange servers so I've decided to use that.
I also found the Exchange Web Services Managed API (using version 1.2.1) which definitely seems to make the task of calling these web services much easier.
My question is, does anyone have any experience in creating automated unit/integration tests using the Managed API?
At the moment I have no Exchange server so really I'd like to create some kind of mock (I usually use Moq) but Microsoft.Exchange.WebServices.Data.ExchangeService
doesn't implement any kind of interface that I can mock. My code is all coded to interface and designed for dependency injection, but I can't think of a good way to abstract out the EWS API dependency.
You visit OWA to check your email on the go. EWS is a different concept. This is a backend, and non-end user facing service tool exposed to the internet so applications, such as Deskpro, can connect into Office365 or an private Exchange system to relay email.
Exchange Web Services (EWS) is a cross-platform API that enables applications to access mailbox items such as email messages, meetings, and contacts from Exchange Online, Exchange Online as part of Office 365, or on-premises versions of Exchange starting with Exchange Server 2007.
Client applications — Standalone applications that use EWS to access Exchange data. Outlook and Outlook Web App are examples of client applications. Portal applications — Applications that extend an existing web page by including information retrieved from Exchange, such as free/busy or contact information.
You can verify independently if a mailbox is accessible using EWS with the following steps: Go to https://testconnectivity.microsoft.com. If using Microsoft 365, click the Microsoft 365 tab. Select Service Account Access (Developers) then click Next.
You can use the Facade design pattern and build a set of classes on top of the EWS Managed API. The Facade classes should implement a set of interfaces that you create yourself. The interfaces should not mimic the EWS API but only expose the functionality you need in your application.
The code in your application would be happily oblivious of EWS. It will only know your Facade interfaces which makes it possible for you to stub or mock the interfaces using Moq when unit testing.
For example if you need to retrieve all items in a mailbox you can create an IMailBox interface with a single method called GetItems:
public interface IMailBox
{
IEnumerable<MailItem> GetItems(string smtpAddress, WellknownFolderName folder);
}
For your application you would then create a class implementing this interface. You can inject the class in your code with your favourite DI framework.
public class MailBox : IMailBox
{
public IEnumerable<MailItem> GetItems(string smtpAddress, WellknownFolderName folder)
{
var service = new ExchangeService();
// Some code here...
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With