-2

I am mocking InetAddress, When I am mocking this I am getting mocked object instead of my expected string

    InetAddress inetAddress = Mockito.mock(InetAddress.class);
    Mockito.when(inetAddress.getHostName()).thenReturn("test")
    System.out.printinetAddress.getHostName());

I think It should print test in a console but it is printing mocked object hashcode.

Ravat Tailor
  • 1,193
  • 3
  • 20
  • 44
  • No it should return test if the method `inetAddress.getHostName()` is called – Jens Jan 24 '19 at 08:48
  • Read the javadoc and see that `Mockito.when(inetAddress.getHostName()).thenReturn("test")` returns an object of class `OngoingStubbing` https://static.javadoc.io/org.mockito/mockito-core/2.23.4/org/mockito/stubbing/OngoingStubbing.html#then-org.mockito.stubbing.Answer- – Jens Jan 24 '19 at 08:58

1 Answers1

0
Mockito.when(inetAddress.getHostName()).thenReturn("test")

With this line, you are saying mockito that when I call the inetAddress.getHostName() return me "test". You have done this declaration however, you have not call this mocked method to see what happens,instead you are printing mockito object which refers to an address like all other objects do in java. To see results, after creation of mocking object you should:

System.out.println(inetAddress.getHostName());
Y.Kakdas
  • 833
  • 7
  • 17