【问题标题】:How to write JUnit Test case如何编写 JUnit 测试用例
【发布时间】:2018-06-21 08:57:32
【问题描述】:

我正在学习 Spring Boot 应用程序的 Junit 测试。我的帐户控制器方法取决于服务类方法。为此,我使用了Mockito。我尝试了简单但是在这里我没有得到如何为以下方法编写测试用例?我如何使用模拟。

谁能帮我写这个测试用例?

帐户控制器

 @RestController
    @RequestMapping("/spacestudy/$ {InstituteIdentifier}/admin/account")
    public class AccountController {

        @Autowired
        AccountService accService;  

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

    }

账户服务

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)
public class TestAccountController {

    private MockMvc mockMvc;

    @Mock
    private AccountService accountService;

    @InjectMocks
    private AccountController accountController;

    @Before
    public void setup() {
        mockMvc = MockMvcBuilders.standaloneSetup(accountController).build();
    }

@Test
    public void populateGridViewsTest() throws Exception {

   //????
   //????

}
}

【问题讨论】:

    标签: junit spring-data-jpa mockito


    【解决方案1】:

    会是这样的:

    @RunWith(SpringRunner.class)
    public class TestAccountController {
    
        private MockMvc mockMvc;
    
        @Mock
        private AccountService accountService;
    
        @InjectMocks
        private AccountController accountController;
    
        @Before
        public void setup() {
            mockMvc = MockMvcBuilders.standaloneSetup(accountController).build();
        }
    
        @Test
        public void populateGridViewsTest() throws Exception {
            when(accountService.populateGridViews("foo","bar")).thenReturn(Arrays.asList(new Tuple("bazz"));
            mockMvc.perform(get("/spacestudy/STACKOVERFLOW/admin/account/foo/bar"))
                   .andExpect(status().isOk())
                   .andExpect(jsonPath("someField").value("bazz"));
    
        }
    }
    

    所以基本上你正在用模拟替换你的服务并说明它应该返回什么。在您的特定情况下,我看不到对此类进行单元测试的任何理由,因为它内部没有任何逻辑。但如果你有类似的东西:

         @GetMapping("/findAccountData")
         public ResponseEntity<List<Tuple>> populateGridViews(...) throws Exception {
           List<Tuple> result = accService.populateGridViews(...);
           if(result==null){
             return ResponseEntity.notFound();
           }
           return  ResponseEntity.ok(result);
         }
    

    那么测试这个类会更有意义,例如

    第一次测试 - 模拟您的 accService 以返回 null 并验证响应状态为 404

    第二次测试 - 模拟您的 accService 以不返回 null 并验证响应状态为 200

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多