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

Raphael Ferreira :

I have an endpoint and I created an AOP Around that will measure the execution time of my endpoint and call an async service that will record that time in the database. This service is already being independently tested. I already have an integration test for my endpoint and I would like at the end of it to check only if my service that is in AOP was actually called. How can I do this?

My Endpoint:

@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;
}

My 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;
    }
}

My Integration Test:

@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

}
Raphael Ferreira :

I was able to solve using the example that @Bond-JavaBond posted.

My Test:

@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();
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=162530&siteId=1