【问题标题】:Get java.lang.NullPointerException when tried to mock private method with mockito and powermock尝试使用 mockito 和 powermock 模拟私有方法时获取 java.lang.NullPointerException
【发布时间】:2013-01-06 04:21:02
【问题描述】:

我正在尝试使用 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)

【问题讨论】:

  • 我找出 NullPointerException: 只需将 PowerMockito 添加到 spy 方法。 'MockitoBusinessOperation = PowerMockito.spy(MockitoBusinessOperation);'现在的问题是模拟私有方法虽然我已经模拟了私有方法但它仍然被调用。 In another post 有人发布了这个问题。但解决方案对我不起作用。任何关于为什么模拟没有得到反映的建议。
  • 你在使用 doWhen() 风格的模拟吗?您可以发布新的代码示例或修改您的问题吗?

标签: java unit-testing testng mockito powermock


【解决方案1】:

您必须使用PowerMockito.spy() 而不是Mockito.spy()

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

【讨论】:

  • 很容易混淆它。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-17
  • 2012-05-18
  • 2018-10-19
  • 2014-02-03
  • 2015-11-04
  • 2015-12-13
相关资源
最近更新 更多