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?
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 - ie sometimes facade pattern with bigger interface combining several of third party interfaces may be better suited for your project.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.