The InetAddress constructor is not visible because the factory pattern is used.
final InetAddress anyInstance = InetAddress.getLocalHost();
new NonStrictExpectations(InetAddress.class) {
{
anyInstance.getHostAddress();
result = "192.168.0.101";
}
};
When I try to use the factory method to get an instance for partial mocking I get the error:
java.lang.IllegalStateException: Missing invocation to mocked type at this point; please make sure such invocations appear only after the declaration of a suitable mock field or parameter
You need to specify that InetAddress
and any subclasses should be mocked:
@Test
public void mockAnyInetAddress(@Capturing final InetAddress anyInstance)
throws Exception
{
new Expectations() {{
anyInstance.getHostAddress(); result = "192.168.0.101";
}};
String localHostAddress = InetAddress.getLocalHost().getHostAddress();
assertEquals("192.168.0.101", localHostAddress);
}