AOP + JUnit how to test?

As everything, if something depends on spring context then it is necessary to brings it up,
Example is simple because i wanted to prepare demo to simulate legacy Validation functionality that i found in client project.

What i used? Spring boot + aop starter + lombok.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {TestAspect.COnf.class})
public class TestAspect extends AbstractJUnit4SpringContextTests {

    @Configuration
    @ComponentScan(basePackages = {"com.example.demo"})
    @EnableAspectJAutoProxy
    public static class COnf {
        @Bean
        ServiceWithAspect serviceWithAspect() {
            ServiceWithAspect serviceWithAspect = new ServiceWithAspect();
            return serviceWithAspect;
        }

        @Bean
        SpringAspectHack springAspectHack() {
            SpringAspectHack serviceWithAspect = new SpringAspectHack();
            return serviceWithAspect;
        }
    }

    @Autowired
    ServiceWithAspect serviceWithAspect;
    @Autowired
    SpringAspectHack springAspectHack;

    @Test
    public void testIfAspectWorking() {
        String ret = serviceWithAspect.olaboga("W"); //WORKS
        Assert.assertThat(ret, Matchers.is("X"));
    }

    @Test
    public void testIfNonAspectWorking() {
        String ret = serviceWithAspect.nonAspect("W"); //AOP Not working
        Assert.assertThat(ret, Matchers.is("W"));
    }

    @Test
    public void testIfAspectWorkingFromHigherLayer() {
        String ret = springAspectHack.higherLevel("W"); //WORKS AOP through higher layer
        Assert.assertThat(ret, Matchers.is("X"));
    }

    @Test
    public void testIfAspectWorkingFromHigherLayerNonAspect() {
        String ret = springAspectHack.higherLevelNonAspect("W"); //AOP not working
        Assert.assertThat(ret, Matchers.is("W"));
    }
}

Aspect handler:

@Slf4j
@Aspect
@Component
public class AscpectHandler {

    @Around("@annotation(MyAspect)")
    public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
        log.debug("DDDDDDDDDDDDDDDDDDDDDDDDDDdd");
        return joinPoint.proceed(new Object[]{"X"});
    }

}

annotation:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAspect {
}

Simple dummy service:

@Slf4j
@Component
public class ServiceWithAspect {

    @MyAspect
    public String olaboga(String x){
        log.debug("ASdfasdfkjsdnafkj");
        return x;
    }


    public String nonAspect(String x){
        log.debug("FFFFFFFFFFFFFFFFFFFFFFFFF");
        return olaboga(x);
    }
}

Higher layer:

@Slf4j
@Component
public class SpringAspectHack {

    @Autowired
    private ServiceWithAspect serviceWithAspect;

    public String higherLevel(String x) {
        log.debug("HIGHER");
        return serviceWithAspect.olaboga(x);
    }
    public String higherLevelNonAspect(String x) {
        log.debug("HIGHER NON");
        return serviceWithAspect.nonAspect(x);

    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s