Junit:org.mockito.exceptions.misusing.MissingMethodInvocationException:

Junit:org.mockito.exceptions.misusing.MissingMethodInvocationException:,junit,spring-data-jpa,mockito,querydsl,Junit,Spring Data Jpa,Mockito,Querydsl,我正在尝试为控制器方法编写测试用例。但是我在org.mockito.exceptions.misusing.MissingMethodInvocationException中遇到了错误,有谁能告诉我为什么会遇到这个异常,我在测试用例中做错了什么 帐户控制器依赖于帐户服务,我正在使用mockito。为了在帐户服务中实现populateGridView(),我正在使用querydsl AccountController @GetMapping("/findAccountData") publ

我正在尝试为控制器方法编写测试用例。但是我在
org.mockito.exceptions.misusing.MissingMethodInvocationException
中遇到了错误,有谁能告诉我为什么会遇到这个异常,我在测试用例中做错了什么

帐户控制器依赖于帐户服务,我正在使用mockito。为了在帐户服务中实现
populateGridView()
,我正在使用
querydsl

AccountController

@GetMapping("/findAccountData")
    public ResponseEntity<List<Tuple>> populateGridViews(@RequestParam(value="sClientAcctId",required=false) String sClientAcctId,
                                                         @RequestParam(value="sAcctDesc",required=false) String sAcctDesc,
                                                         @RequestParam(value="sInvestigatorName",required=false)String sInvestigatorName,
                                                         @RequestParam(value="sClientDeptId",required=false) String sClientDeptId) throws Exception {
        return  ResponseEntity.ok(accService.populateGridViews(sClientAcctId, sAcctDesc,sInvestigatorName,sClientDeptId));
    }

您正在使用When-then方法。 when().thenReturn()的语法为

when(mockedObject.methodName(parameter1,…,parameterN.),然后return(value)

应使用注释模拟AccountService。它不应自动连接

@Mock
private AccountService accountService;
另一件需要注意的事情是MockMvc对象不需要任何注释

private MockMvc mockMvc;
创建一个MockMvc对象就足够了。
希望对您有所帮助……

您能和我分享一下堆栈跟踪吗?我添加了堆栈跟踪。我不确定mockito的对象返回是否正确
。然后返回(accountObj);
    Wanted but not invoked:
accountService.populateGridViews(
    "1122",
    "SRI",
    "Ram",
    "1200"
);
-> at com.spacestudy.controller.AccountControllerTest.populateGridViewsTest(AccountControllerTest.java:67)
Actually, there were zero interactions with this mock.

    at com.spacestudy.controller.AccountControllerTest.populateGridViewsTest(AccountControllerTest.java:67)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Mockito.when(accountService.populateGridViews(sClientAcctId, sAcctDesc, sInvestigatorName, sClientDeptId))
                .thenReturn(accountObj);
@Mock
private AccountService accountService;
private MockMvc mockMvc;