【问题标题】:How can I test that my async process is submitted properly?如何测试我的异步进程是否正确提交?
【发布时间】:2019-11-21 16:22:06
【问题描述】:

我有一个端点,我创建了一个 AOP Around,它将测量我的端点的执行时间并调用一个异步服务,该服务将在数据库中记录该时间。 这项服务已经在进行独立测试。 我已经为我的端点进行了集成测试,我希望在测试结束时仅检查我在 AOP 中的服务是否被实际调用。我该怎么做?

我的端点:

@PostMapping("/doSomething")
@ResponseStatus(HttpStatus.CREATED)
@AuthorizationTime()                                            <--My AOP
public returnVO createSomething(
    @RequestBody @ApiParam(value = "requestVO") final RequestVO requestVO)
    throws Throwable {

    ResponseVO response = doing1();

    doing2();

    return response;
}

我的 AOP:

@Aspect
@Component
@RequiredArgsConstructor
public class TimeAspect {

    @Autowired
    @Qualifier(SleuthThreadConfig.SLEUTH_TASK_EXECUTOR_BEAN_NAME)
    private AsyncTaskExecutor executor;

    private final TransactionAuthorizationTimeService transactionAuthorizationTimeService;

    @Around("@annotation(AuthorizationTime) && args(requestVO)")
    public Object authorizationTime(ProceedingJoinPoint joinPoint, final RequestVO requestVO) throws Throwable {
        final Instant start = Instant.now();

        final Object proceed = joinPoint.proceed();

        final Instant end = Instant.now();

        final int duration = Duration.between(start, end).getNano();

        CompletableFuture
                .runAsync(() -> transactionAuthorizationTimeService.createAuthorizationTimeEntity(
                        requestVO.getKey(),
                        durationTime)
                    , executor);

        return proceed;
    }
}

我的集成测试:

@Test
public void when_create_success() throws JSONException {


    final var vo = prepareVO; 

    RestAssured.given()
        .body(vo)
        //Act
        .contentType(ContentType.JSON)
        .post("/doSomething")
        .then()
        //Assert
        .statusCode(HttpStatus.SC_CREATED)
        .body(not(isEmptyOrNullString()))
        .body(PATH_RESULT, is(SUCESSO.code))
        .body(PATH_DATE_HOUR, not(isEmptyOrNullString()));

//TODO check if my transactionAuthorizationTimeService.createAuthorizationTimeEntity called

}

【问题讨论】:

  • 务实:记录它!;) 通常:您可以(尝试 Power-)模拟 CompletableFuture/执行者 ...
  • 我看到您使用 Lombok 创建构造函数,但我看不到您如何将 TransactionAuthorizationTimeService 连接到方面。无论如何,您可以将包装器(在测试术语中通常称为间谍)连接到集成测试的方面,并确保包装器对感兴趣的方法进行计数或注册调用,然后您可以从测试中检查(异步,即等待或反复轮询一秒钟左右)。
  • 看看here。您可以使用Mockito.verfiy API 系列。
  • 谢谢大家。我能够使用@Bond-JavaBond 发布的示例来解决。
  • 感谢您的提及!然而,我分享了它的@kriegaex 帖子。因此,真正的帮助是他而不是我:)

标签: java asynchronous integration-testing spring-aop junit5


【解决方案1】:

我能够使用@Bond-JavaBond 发布的示例解决问题。

我的测试:

@Autowired
private TimeAspect timeAspect;
@Rule
public MockitoRule mockitoRule = MockitoJUnit.rule();
@Mock
private ProceedingJoinPoint proceedingJoinPoint;

@Test
public void when_create_success() throws JSONException {


    final var vo = prepareVO; 

    RestAssured.given()
        .body(vo)
        //Act
        .contentType(ContentType.JSON)
        .post("/doSomething")
        .then()
        //Assert
        .statusCode(HttpStatus.SC_CREATED)
        .body(not(isEmptyOrNullString()))
        .body(PATH_RESULT, is(SUCESSO.code))
        .body(PATH_DATE_HOUR, not(isEmptyOrNullString()));

    timeAspect.authorizationTime(proceedingJoinPoint, vo);
    verify(proceedingJoinPoint, times(1)).proceed();
}

【讨论】:

  • 感谢您的提及!然而,我分享了它的@kriegaex 帖子。因此,真正的帮助是他而不是我:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-03
  • 1970-01-01
  • 2011-08-04
  • 1970-01-01
  • 2015-08-12
  • 1970-01-01
  • 2010-10-12
相关资源
最近更新 更多