I am attempting to use Junit, mockito and PowerMock to create a unit test
<p>I am attempting to use Junit, mockito and PowerMock to create a unit test</p>

<p>My problem is that one of the classes which i am attempting to mock is returning a null object</p>

<p>This is my code</p>

<pre><code>    @RunWith(PowerMockRunner.class)
@PrepareForTest(ClientBuilder.class)
public class SolrPopulateApplicationTest {

   @Mock
   ClientConfig clientConfig;

   @Mock
   Client client;

   @Mock
   Response response;

   @Mock
   JerseyClient jerseyClient;

   @Mock (answer = Answers.RETURNS_DEEP_STUBS)
   JerseyWebTarget jerseyWebTarget;

   @InjectMocks
   @Autowired
   SolrPopulateApplication solrPopulateApplication;

   @Test
   public void indexTest(){

       PowerMockito.mockStatic(ClientBuilder.class);

       ClientBuilder cli = Mockito.mock(ClientBuilder.class);

    when(ClientBuilder.newClient(Matchers.any())).thenReturn(client);

       when(jerseyClient.target(Matchers.anyString())).thenReturn(jerseyWebTarget);

       when(jerseyWebTarget.path(Matchers.anyString())
               .queryParam(Matchers.anyString(),Matchers.anyString())
       .request(Matchers.anyString())
               .header(Matchers.anyString(),Matchers.anyString())
       .post(Matchers.any())).thenReturn(response);

       boolean var = solrPopulateApplication.index("test","test");

   }
}
</code></pre>

<p>When a debug breakpoint is placed after all the mocks should have been setup i get the following</p>

<pre><code>    client = {Client$$EnhancerByMockitoWithCGLIB$$7f4c6946@1705} "client"
response = {Response$$EnhancerByMockitoWithCGLIB$$b85fdf42@1704} "response"
jerseyWebTarget = {JerseyWebTarget$$EnhancerByMockitoWithCGLIB$$7d7091b9@1703} "null"
solrPopulateApplication = {SolrPopulateApplication@1702} 
jerseyClient = {JerseyClient$$EnhancerByMockitoWithCGLIB$$6437ba91@1701} "jerseyClient"
cli = {ClientBuilder$$EnhancerByMockitoWithCGLIB$$20196b6d@1700} "Mock for ClientBuilder, hashCode: 2080643905"
this = {SolrPopulateApplicationTest@1695}
</code></pre>

<p>as you can see the jerseyWebClient is a NULL and this is causing a nullpointerexception when i try to run the test.</p>

<p>I have tried removing (answer = Answers.RETURNS_DEEP_STUBS) from the @mock statement and this makes no difference.</p>

<p>The method being tested actually calls a interface which implements the JerseyWebTarget class. I have made sure I am trying to mock the right class by putting a debugger in the JerseyWebTarget class to make sure its stopping on the method which is being called via the interface.</p>

<p>can anyone tell me why this is happening and how to fix it.</p>