spring MVC的junit测试用例

时间:2016-08-01 20:28:29

标签: java spring spring-mvc junit

我们正在使用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(..)。

1 个答案:

答案 0 :(得分:2)

您正在尝试编写一个集成测试,其中调用您的控制器,然后调用所有子类,直到触发验证器。这不是传统的“单元测试”。

传统的单元测试只是直接测试验证器,仅此而已。

然而,在编写集成测试时,spring documentation to the rescue
简而言之,它需要您使用所有必需的脚手架创建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;