C# – Unit-Testing functions which have parameters of classes where source code is not accessible

ctestingunit testing

Relating to this question, I have another question regarding unit testing functions in the utility classes:

Assume you have function signatures like this:

public function void doSomething(InternalClass obj, InternalElement element)

where InternalClass and InternalElement are both Classes which source code are not available, because they are hidden in the API. Additionally, doSomething only operates on obj and element. I thought about mocking those classes away but this option is not possible due to the fact that they do not implement an interface at all which I could use for my Mocking classes.

However, I need to fill obj with defined data to test doSomething.

How can this problem be solved?

Best Answer

What you are looking for is the adapter pattern: http://www.dofactory.com/Patterns/PatternAdapter.aspx

Using this pattern you can create a wrapper around the external class.

Lets take the following external class:

public sealed class SomeExternalClassWithoutInterface
{
    private readonly string _someValue;

    public SomeExternalClassWithoutInterface(string someConstructorValue)
    {
        _someValue = someConstructorValue;
    }
    public string Method1()
    {
        return _someValue;
    }

    public int Method2()
    {
        return _someValue.Length;
    }
}

We can now easily create our own class as a wrapper around this external class, and Implement an interface:

public class ExternalClassAdapter : IExternalClassAdapter
{
private readonly SomeExternalClassWithoutInterface _someExternalClassWithoutInterface;

// DI implementation, this is not neccesary
public ExternalClassAdapter(SomeExternalClassWithoutInterface someExternalClassWithoutInterface)
{
    _someExternalClassWithoutInterface = someExternalClassWithoutInterface;
}

// Constructor Without Depedency Injection, this is not preferable.
public ExternalClassAdapter(string someValue)
{
   _someExternalClassWithoutInterface = new SomeExternalClassWithoutInterface(someValue);
}

public string Method1()
{
    return _someExternalClassWithoutInterface.Method1();
}

public int Method2()
{
    return _someExternalClassWithoutInterface.Method2();
}

}

This is our interface, which is implementing all the methods used in the external class. (If alot of methods are available, seperate the interfaces according to the Interface Segregation principle)

public interface IExternalClassAdapter
    {
        string Method1();
        int Method2();
    }

Now, when this Adapter class is used in your application instead of the external class, you should be able to mock any other implementation pretty easy.

You should never use non interfaced external classes in your code.

Pastebin link (since the code outlining is not working well here): http://pastebin.com/2qN7u3u6

If, for any reason, you also need a facade pattern... You can still wrap these 2 adapters in a single facade! But do not create a single facade on 2 external classes!