0

I am basically trying to create an annotation that provides me the execution time of any method. While running the following code, I am not sure if it is hitting the method. Also, intelliJ suggests that "advice advises no method".

This is what I want it to do -


import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Slf4j
@ConditionalOnExpression("${aspect.enabled:true}")
public class ExecutionTimeTracker {
    @Around(value = "@annotation(TrackExecutionTime)")
    public Object executionTime(ProceedingJoinPoint point) throws Throwable {
        long startTime = System.currentTimeMillis();
        Object object = point.proceed();
        long endtime = System.currentTimeMillis();
        log.info(point.getSignature().getDeclaringTypeName() +
                "." + point.getSignature().getName() +
                " >> Took : " + (endtime - startTime) + "mills");
        return object;
    }

}

This is my @interface


import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Target({ METHOD })
@Retention(RUNTIME)
public @interface TrackExecutionTime {
}

This is the UT to test this -

import org.junit.jupiter.api.Test;

public class ExecutionTimeTrackerTest {

    @Test
    @TrackExecutionTime
    public void someMethod(){
        int[] randomNumbers = {582, 15, 596, 827, 779, 426, 153, 557, 394, 12};
        Arrays.sort(randomNumbers);
        int index = Arrays.binarySearch(randomNumbers, 827);
        Assertions.assertEquals(9, index);

    }
}

I tried changing @Around(value = "@annotation(TrackExecutionTime)") to different locations, tried executing with setting up different log levels too. Nothing seem to work.

1 Answers1

1

The problem is that the aspect is managed by Spring and you're not using Spring in your test. You need to create a Spring Boot integration test for this to work.

Example: imagine you have this service

@Service
class MyService {
    @TrackExecutionTime
    public void doSomething() {
        // just waste some time pretending to do something useful
        for (int i = 0; i < 100_000; i++) {
            Math.sqrt(1000);
        }
        System.out.println("Hello, world");
    }
}

then you need the following integration test (a bit of a pointless test to be honest, but it's just to show you how it works):

@SpringBootTest
@RunWith(SpringRunner.class)
public class IntegrationTest {

    @Autowired
    MyService myService;

    @Test
    public void testAspect() {
        
        myService.doSomething();
    }
}

then you'll see that the aspect is used in your console:

Hello, world
com.example.MyService.doSomething >> Took : 31mills
user2340612
  • 10,053
  • 4
  • 41
  • 66