gpt4 book ai didi

java - 莫基托。我试图模拟的依赖项是 NULL

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

在我试图测试的类中,我有 @Resource 注释的依赖项,我正在尝试编写一些测试并想要注入(inject)该依赖项,我的问题是,在模拟依赖项之后,它是值为null

private SOAPService soapService;
private WebServiceContext webServiceContext = mock(WebServiceContextImpl.class);

@Before
public void setup(){
soapService = new SOAPService(webServiceContext);
}

@Test
public void startUploadFileTest(){
soapService.startUpload("text");
......
/I omitted the remaining code, because I get the NullPointerException/
}

注意。我的测试类用 MockitoJUnitRunner.class

进行注释

编辑 SoapService 实现

@WebService
public class SOAPService{

private final static AtomicInteger id = new AtomicInteger();
private WebServiceContext webServiceContext;

public SOAPService(WebServiceContext webServiceContext){
this.webServiceContext = webServiceContext;
}

@Override
public int startUpload(@WebParam String name) {
String username = Authentication.getUsername(webServiceContext.getMessageContext());

try {
File file = new File(username + "/" + name);
file.createNewFile();
} catch (IOException e) {
LOG.error(e.getMessage(), e);
e.printStackTrace();
}
return id.getAndIncrement();
}
}

java.lang.NullPointerException
at org.example.SOAPService.startUpload(SOAPService.java:97)
at org.example.services.SOAPServiceTest.startUploadFileTest(SOAPServiceTest.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

最佳答案

你是否尝试像这样初始化你的soapService:

@Before
public void setup(){
soapService = new SOAPService();
Mockito.initMocks(soapService);
}

编辑

使用新代码,您还必须模拟静态方法

Authentication.getUsername(...)

为此,您必须使用 PowerMockito。

在类声明之前添加注解@PrepareForTest,如

@RunWith(PowerMockRunner.class) 
@PrepareForTest({Authentication.class})
public class MyTest {
....
}

在准备测试期间模拟对 getUserName 的调用

@Before
public void setup(){
soapService = new SOAPService(webServiceContext);
PowerMockito.when(Authentication.getUsername(Mockito.any())).thenReturn("m‌​yVal‌​ue");
}

关于java - 莫基托。我试图模拟的依赖项是 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31984199/

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