如何模拟方法中的对象方法调用



所以这是我想为创建测试的类和方法

public class RequestController {    
private GetAllInter getAnalytics;//This is an interface
@RequestMapping(value ="",method=RequestMethod.GET)
public String getAllAnalytics(){
log.debug("Processing request for all analytics");
String result;
try {
result = getAnalytics.getAll();
} catch (Exception e) {
result = "Could not retrieve full list of analytics";
log.error(result,e);
}
return result;
}

具体来说,我想模拟getAnalytics.getAll(),这样它就会返回我已经定义的字符串。而不是方法做它的事情。这是我的代码:

public class RequestControllerTest extends Mockito{
private String allAnalyticsStr = "Hello";
@Test
public void testGetAllAnalytics(){
GetAllInter getAll = mock(GetAllInter.class);
when(getAll.getAll()).thenReturn("what");
RequestController requestController = new RequestController();
assert(requestController.getAllAnalytics().equals("Hello"));
}

当我尝试运行测试时,我得到的错误是:

16:17:03.240[main]调试日期。RequestController-处理所有分析的请求16:17:03.248[main]错误日期。RequestController-无法检索分析的完整列表java.lang.NullPointerException:null在达尔。RequestController.getAllAnalytics(RequestController.java:32)~[classes/:na]在达尔。RequestControllerTest.testGetAllAnalytics(RequestControllerTest.java:28)[test-classes/:na]在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)~[na:1.7.0_79]在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)~[na:1.7.0_79]在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)~[na:1.7.0_79]位于java.lang.reflect.Method.ioke(Method.java:606)~[na:1.7.0_79]网址:org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)[junit-4.12.jar:4.12]网址:org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)[junit-4.12.jar:4.12]网址:org.junit.runners.model.FrameworkMethod.invokeExploly(FrameworkMethod.java:47)[junit-4.12.jar:4.12]网址:org.junit.internal.runners.statements.InvokeMethod.eevaluate(InvokeMethod.java:17)[junit-4.12.jar:4.12]网址:org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)[junit-4.12.jar:4.12]网址:org.junit.runners.ParentRunner.runLeaf(ParentRunner:java:325)[junit-4.12.jar:4.12]网址:org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)[junit-4.12.jar:4.12]网址:org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)[junit-4.12.jar:4.12]网址:org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)[junit-4.12.jar:4.12]网址:org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)[junit-4.12.jar:4.12]网址:org.junit.runners.ParentRunner.runChildren(ParentRunner:java:288)[junit-4.12.jar:4.12]网址:org.junit.runners.ParentRunner.access$000(ParentRunner:java:58)[junit-4.12.jar:4.12]网址:org.junit.runners.ParentRunner$2.eevaluate(ParentRunner.java:268)[junit-4.12.jar:4.12]网址:org.junit.runners.ParentRunner.run(ParentRunner.java:363)[junit-4.12.jar:4.12]网址:org.junit.runner.JUnitCore.run(JUnitCores.java:137)[junit-4.12.jar:4.12]网址:com.intellij.junit4.JUnit4DeaTestRunner.startRunnerWithArgs(JUnit4DieTestRunner.java:117)[junit-rt.jar:na]网址:com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter:java:234)[junit-rt.jar:na]网址:com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)[junit-rt.jar:na]在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)~[na:1.7.0_79]在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)~[na:1.7.0_79]在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)~[na:1.7.0_79]位于java.lang.reflect.Method.ioke(Method.java:606)~[na:1.7.0_79]网址:com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)[idea_rt.jar:na]和

java.lang.AssertionError在达尔。RequestControllerTest.testGetAllAnalytics(RequestControllerTest.java:28)在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)位于java.lang.reflect.Method.ioke(Method.java:606)网址:org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)网址:org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)位于org.junit.runners.model.FrameworkMethod.invokeExplorery(FrameworkMethod.java:47)位于org.junit.internal.runners.statements.InvokeMethod.eevaluate(InvokeMethod.java:17)网址:org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)网址:org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)网址:org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)网址:org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)网址:org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)网址:org.junit.runners.ParentRunner$1schedule(ParentRunner.java:71)网址:org.junit.runners.ParentRunner.runChildren(ParentRunnr.java:288)网址:org.junit.runners.ParentRunner.access$000(ParentRunner:58)网址:org.junit.runners.ParentRunner$2.eevaluate(ParentRunner.java:268)网址:org.junit.runners.ParentRunner.run(ParentRunner:363)网址:org.junit.runner.JUnitCore.run(JUnitCores.java:137)网址:com.intellij.junit4.JUnit4DeaTestRunner.startRunnerWithArgs(JUnit4DieTestRunner.java:117)网址:com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)网址:com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)位于java.lang.reflect.Method.ioke(Method.java:606)网址:com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)知道我做错了什么吗?有别的办法吗?是因为我在嘲笑类的接口而不是类本身吗?

在执行测试之前,您需要将RequestController#getAnalytics字段替换为模拟的GetAllInter实例。

GetAllInter getAll = mock(GetAllInter.class);
when(getAll.getAll()).thenReturn("what");

该语句不会影响RequestController的getAnalytics实例行为,直到您将其设置为RequestController的字段。

我发现了类似的问题mockito-模拟接口-抛出NullPointerException

最简单的方法是为RequestController#getAllanalytics提供setter,或者提供采用GetAllInter参数的RequestController构造函数。

但是使用@InjectMocks将是更好的解决方案,因为您不需要修改RequestController类进行测试。

相关内容

  • 没有找到相关文章

最新更新