I have a question for anyone out there using AspectJ. I am trying to use AspectJ in combo with
JUnit to get to simulate the use of mock objects by creating a hash map in my aspect that tracks which class/method combinations are to be stubbed. The other thing I am trying to do is to specify an Exception to be thrown, in order to
test how the class handles certain exceptions. Below is my advice for the exception handling:
pointcut throwException(Object target) : execution(* *.* (..) throws *Exception )&& target (target) ;
pointcut withinThis() : within(MockObject +) || target(MockObject +);
pointcut withinTestCase() : within (AspectTestCase +) || target(AspectTestCase +);
before(Object target) throws Throwable : throwException(target) && !withinThis() && !withinTestCase()
{
String key = target.getClass().getName()
+ "."
+ thisJoinPoint.getSignature().getName();
if (myHashMap.containsKey(key))
{
if (myHashMap.get(key) instanceof Throwable)
{
throw (Throwable) myHashMap.get(key);
}
}
}
I have tried to make the code generic so that basically any method that could possibly throw an exception will run this advice and check to see if the current method is designated to automatically throw the exception. I have a method called setMock(String,Object) in which the first parameter is a string in the form "Class.Method" and the second argument is an object that either specified the return value, or an Exception.
When I try to compile the code in Eclipse, I get some errors stating "can't throw checked exception at this join point..." Yet the objects all compile and the aspect get woven in, and when I run my test, it works perfectly. But I still have these nasty red X's next to my objects. Is this a bug in AspectJ, and has anyone else come across this issue. I really hope to use this aspect to greatly simplify my unit testing, and I am so close to getting it right, but I need to resolve why this is happening.