我正在尝试为Custom Aspect编写Junit测试.这是Aspect Class Snippet:
@Aspect
@Component
public class SampleAspect {
private static Logger log = LoggerFactory.getLogger(SampleAspect.class);
@Around("execution(* org.springframework.data.mongodb.core.MongoOperations.*(..)) || execution(* org.springframework.web.client.RestOperations.*(..))")
public Object intercept(final ProceedingJoinPoint point) throws Throwable {
logger.info("invoked Cutom aspect");
return point.proceed();
}
}
Run Code Online (Sandbox Code Playgroud)
因此,只要关节点与切入点匹配,上述方面就会截获.它的工作正常.
但我的问题是如何对该类进行单元测试.我有以下Junit测试:
@Test(expected = MongoTimeoutException.class)
public void TestWithMongoTemplate() {
//MongoDocument class
TestDocument test = new TestDocument();
ApplicationContext ctx = new AnnotationConfigApplicationContext(TestMongoConfigurationMain.class);
MongoTemplate mongoTemplate = ctx.getBean(MongoTemplate.class);
//this call is being intercepted by SampleAspect
mongoTemplate.save(test);
}
Run Code Online (Sandbox Code Playgroud)
因此,我mongoTemplate.save(test)在Junit中被拦截,SampleAspect因为它匹配切入点.但是,我应该如何确保junits(可能通过声明)SampleAspect在调用该关节点时我正在拦截?
我不能断言返回值,intercept()因为除了执行关节点之外没有什么特别之处.因此,我的Junit无法找到任何区别,无论是由方面执行还是基于返回值的常规执行. …