I use AOP to separate monitor from the bussiness logic. But when I use junit to test my aop code I found that AOP are not triggered when method B is executed but are triggered when method A is executed. And method B calls method A.
my pseudocode likes below :
@Aspect
public class TimeMonitor {
@Pointcut("execution( * MainClass.A(..))")
public void pointA();
@Around("pointA()")
Object monitorA(ProceedingJoinPoint jp ){
try{
jp.proceed();
}catch(Exception e){
logger.error("failed to execute A in TimeMonitor");
}
}
my main logic like below:
public class MainClass{
public String A(){
}
public String B(){
try{
A();//call method A
}catch(Exception e ){
logger.error("failed to execute A in Main class");
}
}
}
Then when I do unit test with Junit:
public TimeMonitorTest{
@Test
public void TestA(){
//test code here
A();
//AOP method monitorA will be triggered;
}
@Test
public void TestB(){
B();
//AOP method monitorA will not be triggered;
}
}
So why monitorA() not be triggered when I test method B in MainClass?
Can anyone help me ?
Thanks!!