Disabling certain aspects during unit test runs

前端 未结 4 2101
我在风中等你
我在风中等你 2021-01-04 01:21

I have integration tests (load context) and unit tests running together. My code does aspectj compile time weaving using spring.

My problem is that my declared advi

4条回答
  •  渐次进展
    2021-01-04 01:51

    The answer is simple: You want to use an if() pointcut expression.


    Update (after the question has also been updated): The originally provided link above should contain enough information, but for what it is worth, a short explanation and a simple example:

    An if() pointcut is a static aspect method returning a boolean. If the return value is true, it means that any combined pointcut like myPointcut() && if() matches as long as myPointcut() matches. For a return value of false the whole combined pointcut does not match, effectively deactivating any advice connected to the pointcut.

    So what can you do in a static if() pointcut?

    • evaluate a static boolean member of some tool class like TestMode.ACTIVE which is only true during unit or integration testing
    • evaluate an environment variable which is only set during testing
    • evaluate a Java system property which is only set during testing
    • and many more things

    If you want to do something fancier (and trickier) and performance is not so important, you can also try to dynamically determine whether the auto-wired aspect member variable equals null or not and only activate your pointcuts if the injected object is actually present. The only problem here is how to determine a member variable from a static method. I have no idea about Spring AOP, but in plain AspectJ there is the helper class Aspects with several overloaded methods named aspectOf(..). Assuming that your aspect is instantiated as a singleton, you could do something like this:

    @Pointcut("if()")
    public static boolean isActive() {
        return Aspects.aspectOf(PerformanceMonitorAspect.class).eventJournalService != null;
    }
    
    // ...
    
    @AfterReturning(pointcut = "isActive() && anyPublicMethod() && inService() && @annotation(eventJournal) && args(entity,..)", returning = "id")
    // ...
    
    @AfterThrowing(pointcut = "isActive() && anyPublicMethod() && inService() && @annotation(eventJournal) && args(entity,..)", throwing="ex")
    // ...
    

提交回复
热议问题