Note that there are some explanatory texts on larger screens.

plurals
  1. POGet java.lang.NullPointerException when tried to mock private method with mockito and powermock
    primarykey
    data
    text
    <p>I am trying to mock a private method with mockito/powermock. I get NullpointerException<br> The simple example I am trying to do is:</p> <p><strong>Actual class</strong></p> <pre><code>import com.siriusforce.plugin.common.PluginSystem; import com.wellsfargo.core.business.service.dp.fulfillment.MockitoBusinessService; public class MockitoBusinessOperationImpl implements MockitoBusinessOperation{ private MockitoBusinessService mockitoBusinessService = PluginSystem.INSTANCE.getPluginInjector().getInstance(MockitoBusinessService.class); private Long Id; public String creditAproved( Long Id){ System.out.println("Came Inside MockitoBusinessOperationImpl"); this.Id = Id; if (this.Id != null){ System.out.println("Inside creditaproved If statement"); String Report = mockitoBusinessService.creditReport(this.Id); System.out.println("Mock Object Injected from test class "+ Report); return Report; } else return "Went to Else Part"; } private String inTestMethod(Long Id){ return "this is working"; } } </code></pre> <p><strong>Test Class:</strong></p> <pre><code>import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; import static org.powermock.api.mockito.PowerMockito.doReturn; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.powermock.core.classloader.annotations.PrepareForTest; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import com.siriusforce.plugin.common.PluginSystem; public class MockitoBusinessServiceTest { @Mock MockitoBusinessService MockitoBusinessService ; @InjectMocks private MockitoBusinessOperation MockitoBusinessOperation = PluginSystem.INSTANCE.getPluginInjector().getInstance(MockitoBusinessOperation.class); private Long Id; @BeforeMethod public void init() { MockitoAnnotations.initMocks(this); this.Id = 123L; } @PrepareForTest(MockitoBusinessOperation.class) @Test(enabled = true) public void testReCalculatePrepaids() throws Exception { MockitoBusinessOperation = spy(MockitoBusinessOperation); doReturn("this will work hopefully").when(MockitoBusinessOperation, "inTestMethod", this.Id); when(MockitoBusinessService.creditReport(this.Id)).thenReturn(new String("Decline by only Me")); String mainReport = MockitoBusinessOperation.creditAproved(this.Id); System.out.println("Indirect Call from actual class MainReport " + mainReport); } } </code></pre> <p>When I try to run this I get a <code>NullPointerException</code>: any suggestion solving this or any other way mocking a private method. I don't want to use the suggestion given for making the method protected which mean changing the actual method. I don't want any changes to the actual </p> <pre><code>class method &lt;failure type="java.lang.NullPointerException"&gt;java.lang.NullPointerException at org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.addAnswersForStubbing(PowerMockitoStubberImpl.java:68) at org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.prepareForStubbing(PowerMockitoStubberImpl.java:123) at org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.when(PowerMockitoStubberImpl.java:91) at com.wellsfargo.core.business.service.dp.fulfillment.MockitoBusinessServiceTest.testReCalculatePrepaids(MockitoBusinessServiceTest.java:54) </code></pre> <p></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload