当前位置: 首页 > 知识库问答 >
问题:

当试图用mockito和powermock模拟私有方法时,获取java.lang.NullPointerException

端木令雪
2023-03-14

我试图用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);

    }
}
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个答案

高弘光
2023-03-14

您必须使用powermockito.spy()而不是mockito.spy()

尝试导入静态org.powermock.api.mockito.powermockito.spy;

 类似资料:
  • 我正在尝试测试下一种方法: 称为PrivateMethod: asyncTask的执行无法在Mockito的测试中完成,所以我需要以某种方式模拟它。我试着用PowerMock来嘲弄私有方法: 这在PowerMockito行(NullPointerException)中给了我一个异常,它说 方法引发了“org.mockito.exceptions.Misusing.UnfinishedStubbin

  • 我正在尝试使用JUnit、Mockito和PowerMock验证对的调用。 下面是我的测试用例: 下面是测试中的代码: 非常有趣的是,这段代码失败时出现:

  • 我想为我拥有的一个类编写一个单元测试。这个类有一个公共方法,在公共方法内部有对同一个类中的私有方法的调用。我想模拟对那些私有方法的调用。类与此类似: 对于我的单元测试,我尝试将PowerMock与Mockito和TestNG一起使用。下面是我对测试SomePublicMethod的测试的尝试: 当我运行这个测试时,我会得到一个异常和一些提示: 我在网上看了一些例子,但是我还没有找到一个专门使用Po

  • 有没有可能的方法来模拟一个私人的最终场? 提前谢谢你们。

  • 问题内容: 我有以下要模拟的Logger,但要验证是否正在调用日志条目,而不是内容。 我想模拟用于LoggerFactory.getLogger()的任何类,但是我找不到如何做到这一点。到目前为止,这是我最终得到的结果: 我想知道: 我可以模拟静态模型以用于任何课程吗? 我只能似乎运行的,因此我似乎无法改变每个方法的特点。有没有解决的办法? 编辑结果: 我以为我已经尝试过了,但没有成功: 但是,谢