gpt4 book ai didi

java - Spring MVC 的 junit 测试用例

转载 作者:行者123 更新时间:2023-12-02 03:26:58 24 4
gpt4 key购买 nike

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

最佳答案

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

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

尽管如此,在编写集成测试时,spring documentation to the rescue
简而言之,它将要求您使用所有必要的脚手架创建一个应用程序上下文,然后使用 mockMvc 调用对创建的应用程序执行 GET。

如果您想测试 validator ,请使用简单的模拟框架:请参阅[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;

关于java - Spring MVC 的 junit 测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38707549/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com