• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

AspectJ question

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
George, sorry I have not seen this before. Is this question still actual?
-Barry
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
George, the following compiles when "*Exception" in the first pointcut is changed to "Throwable". Maybe that gives you a clue (I'm using the monkey see - monkey do technique here, I'm just getting a handle on AspectJ myself . )
 
George Lawniczak
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply. Actually I got the solution from AspectJ in Action. What you need to do is to throw and Exception like MockException that is a runtime exception, but wrapping the intended exception. Then another aspect can trap the MockException, call the getCause() method then within that aspect, set up an if-then block to actually throw the original exception. That solved my problem of making my mock object aspect more generic.
 
Ranch Hand
Posts: 3404
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi George,


--------------------------------------------------------------------------------
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.


May I pick your brains and ask where did you get the idea of using AspectJ and JUnit and what was the motivation ? The possibilities are endless.
regards
 
George Lawniczak
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by HS Thomas:
Hi George,


May I pick your brains and ask where did you get the idea of using AspectJ and JUnit and what was the motivation ? The possibilities are endless.
regards


I found several articles on the Internet:
Test flexibly with AspectJ and mock objects
http://www-106.ibm.com/developerworks/java/library/j-aspectj2/?open&l=007,t=gr

Virtual Mock Objects using AspectJ with JUNIT
http://www.xprogramming.com/xpmag/virtualMockObjects.htm
IMHO this is where AspectJ is going to get the most use. I see using this framework not just for testing, but also for creating quick prototypes and demo applications as well.
-George
 
reply
    Bookmark Topic Watch Topic
  • New Topic