这个单元测试实在不会写了,网上的和我这个情况不同。大神们帮我看看。我想测试那个controller层的search方法,具体怎么写,求教。
service:
package com.etonenet.mlk.service;
public interface MoLogService {
Page<MoLog> page(Specification<MoLog> spec, Pageable pageable);
}
serviceimpl:
@Service
public class MoLogServiceImpl implements MoLogService {
private static Logger logger = LoggerFactory.getLogger(MoLogServiceImpl.class);
@Autowired
private MoLogRepository moLogRepository;
@Override
public Page<MoLog> page(Specification<MoLog> spec, Pageable pageable) {
return moLogRepository.findAll(spec, pageable);
}
}
repository:
public interface MoLogRepository extends BaseRepository<MoLog, Long>{
}
public interface BaseRepository<T, ID extends Serializable> extends
PagingAndSortingRepository<T, Serializable> {
public Page<T> findAll(Specification<T> spec, Pageable pageable);
}
controller:
@Controller
@RequestMapping(value = "/moLog")
public class MoLogController extends ControllerBase<MoLog> {
private static Logger logger = LoggerFactory.getLogger(MoLogController.class);
@Autowired
private MoLogService moLogService;
@RequestMapping(value = "index")
public ModelAndView index() {
ModelAndView mav = new ModelAndView("MOUSERINDEX");
return mav;
}
@RequestMapping(value = "page")
@ResponseBody
public Page<MoLog> search(@RequestParam(defaultValue = "0") Integer pageNumber,
@RequestParam(defaultValue = "20") Integer pageSize, final String appParameter1,
final String sourceAddr, final String moStartTime, final String moEndTime) {
if (pageSize > batchExportCount)
pageSize = 20;
Page<MoLog> page = moLogService.page(new Specification<MoLog>() {
@Override
public Predicate toPredicate(Root<MoLog> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
List<Predicate> predicates2 = new ArrayList<Predicate>();
if (StringUtils.isNotBlank(sourceAddr)) {
predicates2.add(cb.equal(root.get("sourceAddr"), sourceAddr));
}
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
if (StringUtils.isNotBlank(moStartTime)) {
Date startDate = null;
try {
startDate = format.parse(moStartTime);
} catch (ParseException e) {
logger.info(e.getMessage(), e);
}
predicates2.add(cb.greaterThanOrEqualTo(root.get("receiveTime").as(Date.class), startDate));
}
if (StringUtils.isNotBlank(moEndTime)) {
Date endDate = null;
try {
endDate = format.parse(moEndTime);
} catch (ParseException e) {
logger.info(e.getMessage(), e);
}
predicates2.add(cb.lessThanOrEqualTo(root.get("receiveTime").as(Date.class), endDate));
}
if (StringUtils.isNotBlank(appParameter1)) {
predicates2.add(cb.equal(root.get("appParameter1"), appParameter1));
}
List<Order> orders = new ArrayList<Order>();
return query.where(predicates2.toArray(new Predicate[predicates2.size()])).orderBy(orders)
.getGroupRestriction();
}
}, new PageRequest(pageNumber, pageSize));
List<MoLog> list = page.getContent();
return new PageImpl<MoLog>(list, new PageRequest(pageNumber, pageSize), page.getTotalElements());
}
晕,方法都写好了,不知道怎么单元测试?是程序员吗?
新手啊,他这个是spring datajpa 的,我不会写。