想要但不被调用:但是与该模拟交互为零

时间:2018-06-21 13:25:22

标签: spring-data-jpa mockito junit4 querydsl

我正在学习使用Spring Boot进行DrawMarker.getInstance(getActivity()).draw(mMap, new LatLng(lat, Long), R.drawable.ic_map_barber, BarberName); 的测试,我正在尝试为junit类编写测试用例。客户控制器取决于客户服务。我为此使用了AccountController。但是由于与模拟的零交互,我得到了错误

AccountController

mockito

AccountService

@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));
    }

AccountControllerTest

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();       

}

堆栈跟踪

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

        @Autowired
        private MockMvc mockMvc;

        @Mock
        private AccountService accountService;

        @InjectMocks
        private AccountController accountController;

           @Test     
            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)

您在此处收到的消息只是告诉您:您为模拟指定了该方法调用,但是在执行测试时从未发生。

换句话说:您指示模拟框架应调用某个方法,但从未以这种方式调用该方法。

有两个简单的解释:

  • 您的嘲笑规范是错误的
  • 您的生产代码不会转弯

在不向我们展示生产代码的情况下,也只能说这些。