【发布时间】:2016-08-01 20:28:29
【问题描述】:
我们正在使用 spring mvc 框架开发一个应用程序。我已经给出了下面所有的类,请建议如何为下面的场景编写一个 junit 测试用例。
我想为 LPAValidator.java 类的 validateAccount(..) 方法中调用的 validateAccountInformation(requestDTO) 方法编写一个 junit 测试用例。下面是我的 junit 测试用例,后面是 java 类。实际调用来自 LPAController.java,如下面的代码所示。
LPAControllerTest.java
@Test(groups = "manual")
public void submitRequestForLPAAccountTest()
{
// businessCalendar.nextBusinessDay(
// LocalDateHelper.today(), LPAConstants.TWENTY_BUSSINESS_DAYS)
//i want to write the test case for the above commented logic,
//if business days is not equal to twenty days, test should fail.
}
LPAController.java
@RequestMapping(value = "/lpa/{accNumber}/spread, method = RequestMethod.GET)
public @ResponseBody LPAResponseDTO accountSearch(@RequestBody final LPARequestDTO clientrequestBody,
@PathVariable final String accNumber, final HttpServletResponse response)
{
//some logic goes here
final LPAAccountResponse domainResponse = service.submitRequestForLPAAccount(requestBody);
}
LPAServiceImpl.java
@PermitAll
@NonTransactional
public LPAResponse submitRequestForLPAAccount(final LPARequest requestDTO)
{
return lpaRepository.submitRequestForLPAAccount(requestDTO));
}
LPARepository.java
@PermitAll
@NonTransactional
public LPAResponse submitRequestForLPAAccount(final LPARequest requestDTO)
{
//some logic
lpaValidator.validateAccount(requestDTO);
//some logic
}
LPAValidator.java -- 用于验证的 java 类
@component
class LPAValidator{
@Inject
private BusinessCalendar businessCalendar;
void validateAccount(final LPARequest requestDTO) throws Exception {
try {
validateAccountInformation(requestDTO);
} catch(Exception e){
}
}
private void validateAccountInformation(final LPARequest requestDTO) throws Exception{
final accDate lpaAccDate = requestDTO.getLPADate();
final LocalDate twentyBussinessDays = businessCalendar.nextBusinessDay(
LocalDateHelper.today(), LPAConstants.TWENTY_BUSSINESS_DAYS); //i want to write
//test case for this line of code, if business days given is more than twenty test should fail.
//some logic here
}
请建议需要在 LPAControllerTest.java 中添加什么来测试上面讨论的 nextBusinessDay(..)。
【问题讨论】:
-
虽然有讨论,但我认为将 LPAController 写成 LpaController 更常见,只大写新词,而不是缩写(即使它们是)。当然你会有 HTTPServlet(或 HttpServlet)有点破坏它,但是......
标签: java spring spring-mvc junit