Testing with a Compiled Class that Doesn’t Implement an Interface – Adapter Pattern

*UPDATE: Per sean chambers, this is an example of the adapter pattern

I recently ran into an issue where I needed to implement a simple email service to send users a randomly generated PIN when they are first entered into the system. To accomplish this I decided to just use the System.Net.Mail implementation.  To create and send an email you have to use the SmtpClient class which does not implement an interface. All I really wanted to test was that the Send() method was called, I did not want to write an integration test that actually sends an email.

One way to work around this problem is to create an interface containing the elements you need to mock from the compiled class.  After this, create your own class that inherits the compiled class and implements your interface. Now when testing, you can seemingly mock up the non-interfaced compiled class, which is exactly what I wanted to achieve. I am not sure whether this is the appropriate way to handle the issue, if anyone has any thoughts on a better way to do this, I would be grateful for the advice.

My specification ended up looking like this:

public class EmailServiceSpecs : ContextSpecification
	protected IEmailService _emailService;
	protected ISmtpClient _smtpClient;
	protected string _emailTo = "phillip.fry@planetexpress.com";
	protected string _emailFrom = "hermes.conrad@planetexpress.com";
	protected string _emailSubject = "New Process to Improve Morale";
	protected string _emailBody = "From now on all employees will be required to have Brain slugs, remember, a mindless worker is a happy worker.";

	protected override void SharedContext()
		DependencyInjection.RegisterType<IEmailService, EmailService>();

		_emailService = DependencyInjection
			.GetDependency<IEmailService>(_emailTo, _emailFrom, _emailSubject, _emailBody);

		_smtpClient = MockRepository.GenerateMock<ISmtpClient>();


[Concern("Email Service")]
public class when_sending_an_email : EmailServiceSpecs
	protected override void Context()
		_smtpClient.Stub(smptClient => smptClient.Send(new MailMessage()))


	public void should_send_email()
			(smtpClient => smtpClient.Send(new MailMessage()),
			assertionOptions => assertionOptions.IgnoreArguments());

Below are my email classes:

public interface ISmtpClient
	void Send(MailMessage message);

public class SubsideSmtpClient : SmtpClient, ISmtpClient { }

public interface IEmailService
	void Send();

public class EmailService : IEmailService
	public EmailService(string to, string from, string subject, string body)
		Email = new MailMessage(from, to, subject, body);

	protected MailMessage Email
		get; set;

	private ISmtpClient _smptClient;

	protected ISmtpClient Smtp
			_smptClient = DependencyUtilities
			return _smptClient;

	public void Send()

Related Articles:

Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Sean Biefeld

Hello all, my name is Sean Biefeld. I graduated from Baylor University with a BBA in Management Information Systems. I am currently working for Headspring . I have been a developing software professionally since 2004. My primary development focus is on HTML, CSS, JavaScript and C#. This blog is a forum for me to post my experiences, ideas, rants, and thoughts as I traverse the mountain of life.
This entry was posted in Adapter Patter, C#, Compiled Class, Implement Interface, Specification, Unit Test. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://www.lostechies.com/members/schambers/default.aspx schambers

    That’s the easiest way to mock portions that you don’t control out of the box. It’s the adapter pattern.

  • http://www.lostechies.com/members/seanbiefeld/default.aspx Sean Biefeld


    Cool, I was wondering if was a pattern, thanks for the info.