What about something in this direction, basically keep your aspect, the internal aspect, delegate behavior to another interface and mock this interface for your tests, instead of mocking the aspect itself. Here is the pseudo code:
public interface ClassLoadHelper{
void processStaticInitialization(Class<?> clazz);
}
public class ClassLoadHelperImpl implements ClassLoadHelper{
private Repository repository;
public ClassLoadHelperImpl() {
repository = OwlApiRepository.getInstance();
}
void processStaticInitialization(Class<?> clazz){
if (type.isInterface()) {
this.repository.storeInterfaceInitialization(type);
} else if (type.isEnum()) {
this.repository.storeEnumInitialization(type);
} else {
this.repository.storeClassInitialization(type);
}
}
}
@Aspect
public class ClassLoadAspect {
private ClassLoadHelper classLoadHelper;
@After("anyStaticInitialization()")
public void processStaticInitilization(JoinPoint jp) {
Class<?> type = jp.getSourceLocation().getWithinType();
this.classLoadHelper.processStaticInitialization(type);
}
@Pointcut("staticinitialization(*) && !within(cz.cvut.kbss.odra..*)")
public void anyStaticInitialization() {
}
public ClassLoadHelper getClassLoadHelper() {
return classLoadHelper;
}
public void setClassLoadHelper(ClassLoadHelper classLoadHelper) {
this.classLoadHelper = classLoadHelper;
}
}
Now in your test you can do this:
ClassLoadAspect.aspectOf().setClassLoadHelper(mockClassLoadHelper);
source
share