尝试使用mockito和powermock模拟私有方法时获取java.lang.NullPointerException

时间:2013-01-22 19:59:38

标签: java unit-testing testng mockito powermock

我正在尝试使用mockito / powermock来模拟私有方法。我得到NullpointerException
我想要做的简单例子是:

实际课程

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

测试类:

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

    }
}

当我尝试运行此操作时,我得到NullPointerException:任何解决此问题的建议或以任何其他方式模拟私有方法。我不想使用给出保护方法的建议,这意味着改变实际方法。我不希望对实际的

进行任何更改
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)

1 个答案:

答案 0 :(得分:11)

您必须使用PowerMockito.spy()代替Mockito.spy()

尝试导入static org.powermock.api.mockito.PowerMockito.spy;