6
votes

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.

1
Have you ever found an answer?Thomas
@Thomas Unfortunately not. The EWS API doesn't implement interfaces so you can't mock them out. All I did in the end was set up a test Exchange Server and perform integration tests against it unfortunately. As you can imagine, they're a bit slow and a bit brittle but acceptable given lack of alternatives.Adam Rodger

1 Answers

5
votes

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...
    }
}