简体   繁体   English

Spring MVC的junit测试用例

[英]junit test case for spring MVC

we are developing an application using spring mvc framework. 我们正在使用Spring MVC框架开发应用程序。 I have given all the classes below, please suggest how to write a junit test case for the below scenario. 我已经给出了下面的所有类,请建议如何为以下场景编写一个junit测试用例。

I want to write a junit test case for the validateAccountInformation(requestDTO) method which is called in validateAccount(..) method of LPAValidator.java class. 我想为validateAccountInformation(requestDTO)方法编写一个junit测试用例,该方法在LPAValidator.java类的validateAccount(..)方法中调用。 Below is my junit test case followed by the java classes. 下面是我的junit测试用例,后面是java类。 Actual call goes from the LPAController.java as shown in the below code. 实际调用来自LPAController.java,如以下代码所示。

LPAControllerTest.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 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 LPAServiceImpl.java

@PermitAll
@NonTransactional
public LPAResponse submitRequestForLPAAccount(final LPARequest requestDTO)
{
    return lpaRepository.submitRequestForLPAAccount(requestDTO));
}

LPARepository.java LPARepository.java

    @PermitAll
    @NonTransactional
    public LPAResponse submitRequestForLPAAccount(final LPARequest requestDTO)
    {
    //some logic
    lpaValidator.validateAccount(requestDTO);

    //some logic

    }

LPAValidator.java -- java class for validations 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
        }

Please suggest what needs to be added in LPAControllerTest.java to test the nextBusinessDay(..) as discussed above. 请建议需要在LPAControllerTest.java中添加哪些内容,以测试如上所述的nextBusinessDay(..)。

You're trying to write an integration test in which your controller is called, which then calls all the subclasses until the validator is triggered. 您试图编写一个集成测试,在该测试中调用您的控制器,然后调用所有子类,直到触发验证器。 That is not a traditional 'unit test'. 那不是传统的“单元测试”。

A traditional unit test would just test the validator straight up, and nothing more. 传统的单元测试只会直接验证验证器,仅此而已。

Nevertheless, when writing an integration test, spring documentation to the rescue 不过,在编写集成测试时,需要提供弹簧文档
In short, it'll require you to create an applicationcontext with all the necessary scaffolding, and then use a mockMvc call to do a GET on the created application. 简而言之,它将要求您使用所有必要的脚手架创建一个applicationcontext,然后使用mockMvc调用对创建的应用程序执行GET。

If you want to test the validator, use simple mocking framework: See [ http://mockito.org] Gives you something like this: 如果要测试验证器,请使用简单的模拟框架:请参阅[ 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM