[英]junit test case for spring MVC
我們正在使用Spring MVC框架開發應用程序。 我已經給出了下面的所有類,請建議如何為以下場景編寫一個junit測試用例。
我想為validateAccountInformation(requestDTO)方法編寫一個junit測試用例,該方法在LPAValidator.java類的validateAccount(..)方法中調用。 下面是我的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(..)。
您試圖編寫一個集成測試,在該測試中調用您的控制器,然后調用所有子類,直到觸發驗證器。 那不是傳統的“單元測試”。
傳統的單元測試只會直接驗證驗證器,僅此而已。
不過,在編寫集成測試時,需要提供彈簧文檔
簡而言之,它將要求您使用所有必要的腳手架創建一個applicationcontext,然后使用mockMvc調用對創建的應用程序執行GET。
如果要測試驗證器,請使用簡單的模擬框架:請參閱[ http://mockito.org]為您提供以下內容:
@Mock BusinessCalendar businessCalendarMock;
@Mock LPARequest mockRequest;
@Mock accDate mockDate;
@Mock LocalDate mockLocalDate;
@InjectMocks LPAValidator lpaValidator = new LPAValidator();
@Test public void testValidateAccount() {
when(mockRequest.getLPAdate()).thenReturn(mockDate);
when(businessCalendar.nextBusinessDay(LocalDateHelper.today(),LPAConstants.TWENTY_BUSSINESS_DAYS).thenReturn(mockLocalDate);
// continue your test from here
lpaValidator.validateAccount( mockRequest);
verify(businessCalendar).nextBusinessDay(LocalDateHelper.today(),LPAConstants.TWENTY_BUSSINESS_DAYS);
// although if the use of mockLocalDate is integral to your code, it'll probably show before and no verify is necessary;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.