Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] AspectJ Unit Testing


Cosmin Mogos schrieb:
Hi,

Is there a tool for AspectJ unit testing? I see a project on sourceforge named aUnit but it does not seem to be maintained.
hi,

i recently had the same problem to test an around advice, which i solved by using aspectj annotations to have access to aspect as a usual java class. then i used static dependency injection to mock dependencies. i used static fields and static setters for dependency injection because i could not figure out how to instantiate the aspect itself (maybe that is not possible for it is done behind the scenes by aspectj runtime).

further more i implemented a test-class inside unit-test which was a weave target, so the only production code i tested was the aspect-code itself. the rest had all been test stubs or dummies. so have a look (below is partly pseudo code).

class AroundAdviceAspectTest extends TestCase{
...
   //to verify calls to proceed
   boolean proceedCalled=false;

   class TestClassToBeAspected{
@AnnotationForPointcut
      void callMe(String param){
         proceedCalled=true;
       }

   }

   public void testAroundAdviceCallProceed(){
      //setup
      ..... //initialization of mocks
      AspectImplClass.setDep1(mockFromEasyMock1);
      AspectImplClass.setDep2(mockFromEasyMock2);
      replay(....)
TestClassToBeAspected aspectTarget=new TestClassToBeAspected(); //exercise
      target.callMe("...");
      //verify
assertTrue(aspectTarget.proceedCalled); verify(....); //here you verify, that aspect-weaved code got called
   }
//example, why proceedCall flag makes sense
   public void testIgnoreProceedBecauseOfException(){
      //similar to above test, but mocks throwing exception somewhere...
assertFalse(aspectTarget.proceedCalled); verify(....); }
}

with this unit test pattern i already found errors before the aspect got in place in the production system. without it i definitely would have needed much longer time (coarse grained integration test etc.). so go for it. aspects are more tricky to test but it is possible.

hope this gave you some ideas.
maybe i will have a more detailed entry in my blog soon, will tell you then.

--
manuel aldana
aldana@xxxxxx software-engineering blog: www.aldana-online.de



Back to the top