如何测试我的异步进程是否已正确提交?

How can I test that my async process is submitted properly?

我有一个端点,我创建了一个 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

}

我能够使用@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();
}