gpt4 book ai didi

mockito - org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.addAnswersForStubbing 处的 java.lang.NullPointerException

转载 作者:行者123 更新时间:2023-12-02 09:31:25 25 4
gpt4 key购买 nike

异常

java.lang.NullPointerException 
at org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.addAnswersForStubbing(PowerMockitoStubberImpl.java:67)
at org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.when(PowerMockitoStubberImpl.java:42)
at org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.when(PowerMockitoStubberImpl.java:105)
at us.ny.state.ij.safeact.ask.facade.AmmoSellerKeeperFacadeBeanTest.setUp(FacadeBeanTest.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at

模拟代码

BusinessServiceFactory serviceFactory = BusinessServiceFactory.getInstance();
RegBusinessServiceImpl regCreateService =
serviceFactory.getRegBusinessService(adrEntityManager);

测试代码

@RunWith(PowerMockRunner.class)
@PrepareForTest({ BusinessServiceFactory.class})
public class FacadeBeanTest {

@Before
public void setUp() throws Exception {
AmmoSellerRegBusinessServiceImpl mockRegBusinessServiceImpl
= mock(AmmoSellerRegBusinessServiceImpl.class);
PowerMockito.doReturn(mockRegBusinessServiceImpl)
.when(BusinessServiceFactory.class,"getRegBusinessService",
(mockEntityManager)); //--- line 84 null pointer exception
}
}

我不明白为什么我会看到异常。如果有任何建议,我将不胜感激。

最佳答案

仅供引用:

解决方案是使用 PowerMockito.mock() 而不是 Mockito.mock()


你应该做的

AmmoSellerRegBusinessServiceImpl  mockRegBusinessServiceImpl 
= PowerMockito.mock(AmmoSellerRegBusinessServiceImpl.class);

代替

AmmoSellerRegBusinessServiceImpl  mockRegBusinessServiceImpl 
= mock(AmmoSellerRegBusinessServiceImpl.class);
// assuming your are using Mockito.mock()
// correct me if I am wrong

我也遇到了同样的问题。这个解决方案是我的解决方案。希望对您有所帮助。

关于mockito - org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.addAnswersForStubbing 处的 java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32769572/

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