Arthur Eirich
Arthur Eirich

Reputation: 3638

Mockito: get the value of a field in a mocked object

I have a mocked javax.ws.rs.client.WebTarget webTarget. Then I do following:

Mockito.when(webTarget.path(Mockito.anyString())).thenReturn(webTarget).

Now I'd like to retrieve the String set during the .path(String) method out of the webTarget mock. Is there any chance I can do that? At the time

webTarget.getUri()

unfortunately returns null.

Upvotes: 2

Views: 1853

Answers (1)

roby
roby

Reputation: 3273

To get at the string you could use ArgumentCaptor:

ArgumentCaptor<Person> argument = ArgumentCaptor.forClass(Person.class);
verify(mock).doSomething(argument.capture());
assertEquals("John", argument.getValue().getName());

or verify

Mockito.verify(webTarget).path("expectedString")

Upvotes: 3

Related Questions