gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:56:02 26 4
gpt4 key购买 nike

我正在尝试使用 mockito/powermock 模拟私有(private)方法。我得到 NullpointerException
我尝试做的简单示例是:

实际类(class)

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:任何解决这个问题的建议或任何其他模拟私有(private)方法的方法。我不想使用为保护方法而给出的建议,这意味着更改实际方法。我不想对实际有任何改变

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)

最佳答案

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

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

关于java - 尝试使用 mockito 和 powermock 模拟私有(private)方法时获取 java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14466991/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com