How to mock out InetAddress.getLocalHost() using JMockit -


the inetaddress constructor not visible because factory pattern used.

final inetaddress anyinstance = inetaddress.getlocalhost();     new nonstrictexpectations(inetaddress.class) {       {         anyinstance.gethostaddress();         result = "192.168.0.101";       }     }; 

when try use factory method instance partial mocking error:

java.lang.illegalstateexception: missing invocation mocked type @ point; please make sure such invocations appear after declaration of suitable mock field or parameter 

you need specify inetaddress and subclasses should 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); } 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -