How to unit test chained method call(s) using Mockito
<p>I am working on a J2EE project which uses the <a href="https://marketplace.atlassian.com/plugins/com.atlassian.jira.jira-rest-java-client/server/overview" rel="noreferrer">JIRA's REST Client</a>.
This client returns a Jira <code>issue</code> object.
Some of the fields of the <code>Issue</code> class are <code>key</code>, <code>self</code>, <code>id</code>, <code>summary</code>, etc etc. 
The <code>self</code> field here is basically a URI.<br>
For Eg <code>http://jira.company.com/rest/api/2.0/issue/12345</code>
I have a use case where I have to retrieve the host from the URI specified above.</p>

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

<p>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.</p>

<p>I have the following code snippet.</p>

<pre><code>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);
    }
</code></pre>

<p>Here, I get a <code>Null Pointer Exception</code>.</p>

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

<p>Can someone tell me how can I mock chained method calls.</p>