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

时间:2018-06-21 11:13:48

标签: junit spring-data-jpa mockito querydsl

我正在尝试为控制器方法编写测试用例。但是我在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));
    }

AccountService

public List<Tuple> populateGridViews(String sClientAcctId, String sAcctDesc, String sInvestigatorName,
        String sClientDeptId)throws Exception{

    QAccount account = QAccount.account;
    QDepartment department = QDepartment.department;
    QAccountCPCMapping accountCPCMapping = QAccountCPCMapping.accountCPCMapping;
    QInvestigator investigator = QInvestigator.investigator;

    JPAQuery<Tuple> query = new JPAQuery<Tuple>(em);
    query.select(Projections.bean(Account.class, account.sClientAcctId, account.sAcctDesc, account.sLocation,
            Projections.bean(Department.class, department.sDeptName, department.sClientDeptId).as("department"),
            Projections.bean(Investigator.class, investigator.sInvestigatorName).as("investigator"),
            Projections.bean(AccountCPCMapping.class, accountCPCMapping.sCCPCode).as("accountCPC"))).from(account)
            .innerJoin(account.department, department).innerJoin(account.accountCPC, accountCPCMapping)
            .innerJoin(account.investigator, investigator);

    if (StringUtils.isNotEmpty(sClientAcctId)) {
        query.where(account.sClientAcctId.equalsIgnoreCase(sClientAcctId));
    }
  // code.......


    return query.fetch();       

}

AccountControllerTest

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class AccountControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @Autowired
    private AccountService accountService;

       @Test
       @Transactional
        public void populateGridViewsTest() throws Exception {

            String sClientAcctId = "1122";
            String sAcctDesc = "SRI";
            String sInvestigatorName = "Ram";
            String sClientDeptId = "1200";      

            Tuple mockedTuple = Mockito.mock(Tuple.class);      

            List<Tuple> accountObj = new ArrayList<>();
            accountObj.add(mockedTuple);
            Mockito.when(accountService.populateGridViews(sClientAcctId, sAcctDesc, sInvestigatorName, sClientDeptId))
                    .thenReturn(accountObj);

            mockMvc.perform(
                    get("/spacestudy/$ InstituteIdentifier/admin/account/findAccountData")
                    .param("sClientAcctId", "1122")
                    .param("sAcctDesc", "SRI")
                    .param("sInvestigatorName", "Ram")
                    .param("sClientDeptId", "1200")
                    .accept(MediaType.APPLICATION_JSON))
                    .andExpect(status().isOk())
                    .andDo(print());        

            Mockito.verify(accountService).populateGridViews(sClientAcctId, sAcctDesc, sInvestigatorName, sClientDeptId);

        }
}

堆栈跟踪

    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)

1 个答案:

答案 0 :(得分:1)

您在此处使用“何时”方法。 when()。thenReturn()的语法为

  

when(mockedObject.methodName(parameter1,..,parameterN).thenReturn(value);

Mockito.when(accountService.populateGridViews(sClientAcctId, sAcctDesc, sInvestigatorName, sClientDeptId))
                .thenReturn(accountObj);

AccountService应该使用注释来模拟。它不应该自动接线。

@Mock
private AccountService accountService;

应该注意的另一件事是MockMvc对象不需要任何注释。

private MockMvc mockMvc;

创建一个MockMvc对象就足够了。 希望对您有帮助...