【问题标题】:junit test case for spring MVCspring MVC的junit测试用例
【发布时间】: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


【解决方案1】:

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

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

不过,在编写集成测试时,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;

【讨论】:

  • 由于我是 spring 新手,你提供的链接让我很困惑。我的应用程序流程与上面提到的 java 类中所示的相同。您能否提供一个代码 sn-p 以在 junit 测试类 Koos Gadellaa 的测试用例方法 submitRequestForLPAAccountTest() 中调用 validateAccountInformation(requestDTO) 方法。谢谢
  • 我想编写一个测试用例来测试 validateAccountInformation(..) 方法中的 businessCalendar.nextBusinessDay(LocalDateHelper.today(), LPAConstants.TWENTY_BUSSINESS_DAYS) 是否给出 20 天作为输入(即, LPAConstants.TWENTY_BUSSINESS_DAYS 应该分配给 20) 。如果给出 20 以外的任何值,则测试应该失败。
  • 如果你想测试 BusinessCalendar.nextBusinessDay() 你为 BusinessCalendar 编写一个 junit 测试用例。在其中,您测试是否为适当的方法引发了异常。在您的验证器中,您可能会注入您的 businesscalendar 对象(如果没有,您应该。使用 spring-DI,或者如果这是不可能的,只需使用 spring-tests ReflectionUtils 模拟覆盖该字段,这样您就可以验证适当的已拨打电话。
  • Koos Gadellaa - 如果可能的话,你能举个例子吗,谢谢。
  • 将您的测试用例与 LPAValidator 放在同一个包中。 request.getLPADate 返回一个 accDate (可怕的名字,但它是你的代码)。所以 mockDate 应该有一个 accDate 作为类型。
猜你喜欢
  • 2018-02-23
  • 2011-08-23
  • 1970-01-01
  • 2015-08-07
  • 2016-04-30
  • 2013-09-25
  • 1970-01-01
  • 1970-01-01
  • 2018-12-24
相关资源
最近更新 更多