Easiest way to achieve it is creating wrapper for this static methods:
public class DnsWrapper : IDnsWrapper
{
public IPHostEntry GetHostEntry(string hostNameOrAddress)
{
Dns.GetHostEntry(hostNameOrAddress);
}
}
And make your code depend on this interface:
public interface IDnsWrapper
{
IPHostEntry GetHostEntry(string hostNameOrAddress);
}
Now mocking of this dependency is very easy with any mocking library. E.g with Moq:
Mock<IDnsWrapper> dnsMock = new Mock<IDnsWrapper>();
dnsMock.Setup(d => d.GetHostEntry(It.IsAny<string>()))
.Throws(new SocketException());
var yourClass = new YourClass(dnsMock.Object); // inject interface implementation
yourClass.DoSomethingWhichGetHostsEntry();