我在执行集成测试用例时遇到了问题。根据以下查询,有日期比较。当testcase执行此查询时,它返回零结果。当我从查询中删除where条件时,它返回正确的结果为2.为了深入挖掘,在我的测试用例中,我首先获取了员工并验证他们有正确的日期并满足where条件。但是当我通过Integration testcase在DAO层中检查失败时,我仍然感到困惑。
以下是EmployeeDAOImpl类
private static final String EMP_COUNT_BY_COMPANY_QUERY = "select count(employeeid) from ref_employee e join txn_user u on (e.createdby=u.userid and u.companyid = :companyId) where e.createddate between :startDate and :endDate";
@Override
public Long getEmpCountByCompany(Long companyId) {
BigInteger recordCount = (BigInteger) getHibernateTemplate().execute(new HibernateCallback() {
@Override
public Object doInHibernate(Session session) throws HibernateException, SQLException {
SQLQuery sqlQuery = session.createSQLQuery(EMP_COUNT_BY_COMPANY_QUERY);
sqlQuery.setParameter("companyId", companyId);
sqlQuery.setParameter("startDate", getStartOfCurrentDate());
sqlQuery.setParameter("endDate", getEndOfCurrentDate());
return sqlQuery.uniqueResult();
}
});
return recordCount.longValue();
}
private Date getEndOfCurrentDate() {
Date end = DateUtils.addDays(new Date(), 1);
return DateUtils.truncate(end , Calendar.DATE);
}
private Date getStartOfCurrentDate() {
return DateUtils.truncate(new Date(), Calendar.DATE);
}
以下是employeeServiceClass
@Override
public Long getEmpCountByCompany(Long companyId) {
return empDao.getEmpCountByCompany(companyId);
}
以下是Junit测试
@Transactional
@DatabaseSetup(value = "EmployeeControllerIT.xml", type = DatabaseOperation.CLEAN_INSERT)
public class EmployeeControllerIT extends SpringMvcIT {
@Autowired
EmployeeService employeeService;
@Before
public void setUpEmployeeControllerIT() {
Set<Long> empIds = new HashSet<Long>();
empIds.add(new Long("416908791"));
empIds.add(new Long("416908790"));
List<Employee> empList = empService.getEmpById(empIds);
for (Employee emp : empList) {
emp.setCreatedDate(new Date());
emp.getAuditInfo().setCreatedBy(user);
employeeService.saveOrUpdateEmployee(emp);
}
}
@Test
public void shouldReturnFalseIncaseEmpCountIsReached2() throws Exception {
mockMvc.perform(
get(EMPLOYEE_VALIDATION).param("action", "isEmployeeAllowed")
.accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()).andExpect(jsonPath("$.isValidCount", is(false)));
}
}
以下是控制器
@Controller
@RequestMapping("/employeeValidation.htm")
public class EmployeeValidationController {
private static final String VALIDATE_EMPLOYEE_ELIGIBILITY = "action=isEmployeeAllowed";
@RequestMapping(method = RequestMethod.GET, params = { VALIDATE_EMPLOYEE_ELIGIBILITY })
public void validateEmployeeEligibility(HttpServletResponse response) throws Exception {
Long count = employeeService.getEmpCountByCompany(Constant.COMPANY_ID)
JSONObject employeeValidationObject = new JSONObject();
boolean flag = true;
if(count>=2) {
flag = false;
}
employeeValidationObject.put("isValidCount", flag);
response.setContentType("application/json");
response.getOutputStream().write(employeeValidationObject.toString().getBytes("UTF-8"));
}
}