Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unit test chained method call(s) using Mockito

I am working on a J2EE project which uses the JIRA's REST Client. This client returns a Jira issue object. Some of the fields of the Issue class are key, self, id, summary, etc etc. The self field here is basically a URI.
For Eg http://jira.company.com/rest/api/2.0/issue/12345 I have a use case where I have to retrieve the host from the URI specified above.

I can do that by something like issue.getSelf().getHost().
issue.getSelf() returns an object of type 'URI' and to get the host I can simply use the getHost() method provided by the URI class which returns the host url in String format.

Everything works fine. I am facing problem in unit testing this piece of code using Mockito. I don't know how to mock chained method calls.

I have the following code snippet.

private static final String JIRA_HOST = "jira.company.com";
@Mock private com.atlassian.jira.rest.client.api.domain.Issue mockIssue;

@Before
    public void setup() {
        when(mockIssue.getSelf().getHost()).thenReturn(JIRA_HOST);
    }

Here, I get a Null Pointer Exception.

After doing much research, I came to know that I will have to use @Mock(answer = Answers.RETURNS_DEEP_STUBS) private com.atlassian.jira.rest.client.api.domain.Issue mockIssue;.
But this also gives me a Null Pointer Exception.

Can someone tell me how can I mock chained method calls.

like image 981
RITZ XAVI Avatar asked Dec 12 '16 21:12

RITZ XAVI


People also ask

How do you mock chain methods in jest?

You can use mockFn. mockReturnThis() to do this. const client = { items: () => { return client; }, type: (name: string) => { return client; }, toObservable: () => { return client; }, subscribe: handler => { handler(); return client; } }; export { client };

How do you call a real method in Mockito?

Mockito allows us to partially mock an object. This means that we can create a mock object and still be able to call a real method on it. To call a real method on a mocked object we use Mockito's thenCallRealMethod().

Does Mockito when call the method?

A mock does not call the real method, it is just proxy for actual implementations and used to track interactions with it. A spy is a partial mock, that calls the real methods unless the method is explicitly stubbed. Since Mockito does not mock final methods, so stubbing a final method for spying will not help.

How does @mock work in Mockito?

With Mockito, you create a mock, tell Mockito what to do when specific methods are called on it, and then use the mock instance in your test instead of the real thing. After the test, you can query the mock to see what specific methods were called or check the side effects in the form of changed state.


1 Answers

You don't need RETURNS_DEEP_STUBS or whatever that mock annotation is. You just have to mock every object that you want to return in the chain, similar to this:

@Mock Issue issue;
@Mock URI uri;

@Before
public void setup() {
    when(uri.getHost()).thenReturn(JIRA_HOST);
    when(issue.getSelf()).thenReturn(uri);
}
like image 117
nickb Avatar answered Nov 14 '22 21:11

nickb