Rockstart
Rockstart

Reputation: 2377

Do I need a wrapper for third party calls that return interface?

Consider I need to call a 3rd party API that looks like

public class ThirdPartyClass : IThirdPartyClass
{
    public IThirdPartyReturnObject ThirdPartyMethod()
    {
        //some code
    }
}

And I am calling it like:

public void MyClass
{    
    private IThirdPartyClass _thirdPartyObject;
    public MyClass(IThirdPartyClass thirdPartyObject)
    {
        _thirdPartyObject = thirdPartyObject;
    }

    public void MyFunction()
    {
        _thirdPartyObject.ThirdPartyMethod();
    }
}

Since the third party class is injected by an interface, I can mock the 3rd party object for unit tests.

Is this design better, or should I introduce a ThirdPartyClassWrapper that encapsulates just the calling of ThirdPartyMethod? Is introducing too many wrappers an anti-pattern?

Upvotes: 1

Views: 75

Answers (1)

Alexei Levenkov
Alexei Levenkov

Reputation: 100545

There is no need to introduce wrapper class - mocking interfaces that come from any place is standard and easy approach for unit tests.

Whether you should do that - depends on your needs - i.e. sometimes facade pattern with bigger interface combining several of third party interfaces may be better suited for your project.

Upvotes: 1

Related Questions