When trying to simulate private methods using mockito and powermock, get Java lang.NullPointerException

I tried to simulate a private method with mockito / powermock I got NullPointerException

Practical courses

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";
    }
}

Test class:

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);

    }
}

When I try to run it, I get a NullPointerException: any suggestion to solve this problem or simulate a private method in any other way I don't want to use suggestions that give protection methods, which means changing the actual methods I don't want anything to change the reality

class method
    <failure type="java.lang.NullPointerException">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)

Solution

You must use powermockito Spy () instead of mockito spy()!

Try importing static org. Org powermock. api. mockito. powermockito. spy;

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>