Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Pointcuts as a type?

I would like to see some actual code that shows the scenario, but can you not just write a more generic aspect that advises any place you might want to return some object and then register with it the joinpoint and the value that should be returned from that join point?

aspect A {

  Map<String, Mock> retvals = ...

  public void registerMockReturn(String thisJoinPointString,Mock mock) {
     retvals.put(thisJoinPointString,mock);
  }

  Object around(): execution(* *(..)) {
     Object val = retvals.get(thisJoinPoint.toString());
     if (val!=null) return val; else return proceed();
  }
}


> So then I could declare all my mocks and pointcuts in the test method
> pass them to the object replacement aspect and also verify the mocks in
> the same method. Therefore having almost all the code in one place.

public void testFoo() {
   Mock simpleMock= new SimpleMock();
   A.aspectOf().registerMockReturn("method-execution(public void someMethod(int))", simpleMock);
   Object o = someMethod();
   assertEquals(o,simpleMock);
 }
     
Maybe I've got the wrong end of the stick - if this just isn't what you are trying to achieve, include some pseudo code showing what you are trying.

cheers,
Andy.

2008/11/4 Piers <piers@xxxxxxxxxxxxxxxxxxxxxxxxx>
Hi Ramnivas

I am unit testing some old code in a project and I am not allowed to
modify the code,many of the functions under test take no parameters and
have return type void, so the way I am testing is to use AspectJ to
replace return values of functions within the function under test with
mocks.

EG

public void functionUnderTest(){

       List x = SomeClass.getData(); <-- at this point I return a mock
                                       - and use it to verify that the
                                       - correct operations are being
                                       - performed on x
       //code that manipulates x
}


Normally when using mocks to test one follows this pattern

1. Declare the mocks
2. Set expectations for the mocks
3. Call the function under test with the mocks
4. Verifies that the mocks have been used as intended

And this is all written within the test method.

If using aspects this code becomes broken up

1. One must declare mocks and expectations in the aspect so that it has
access to them so that it may return them.

2. The test function consists only of a call to the method under test.
(Not immediately clear what is going on for someone not well versed in
aspects).

3. Mocks in the aspect have to be fields since when we want to verify
them we have to write different advice for the point at which the method
under test returns.

Lets say I'm testing a class with 10 methods and I require 3 mocks per
method that's 30 fields in my Aspect, then to try and clean up this mess
I can maybe create an new Aspect for each test method, but that breaks
up the test code even more.

So the use case would be, to be able to declare an aspect with advice
that can take parameters (mocks and pointcuts). So I can declare an
aspect which simply takes a pointcut and an object and returns that
object at the given pointcut.

So then I could declare all my mocks and pointcuts in the test method
pass them to the object replacement aspect and also verify the mocks in
the same method. Therefore having almost all the code in one place.

I hope this is clear.

Piers

Ramnivas Laddad wrote:
A pointcut serves as an intermediate program construct so that AspectJ can weave crosscutting action at join points selected by it.

Can you provide a use case that will benefit from implementing pointcuts as a Java type?

-Ramnivas

On Sat, Nov 1, 2008 at 10:18 AM, Piers Powlesland <piers@xxxxxxxxxxxxxxxxxxxxxxxxx <mailto:piers@xxxxxxxxxxxxxxxxxxxxxxxxx>> wrote:

   Hi All

   I was wondering why pointcuts have been implemented in the way that
   they have and not as a usual java type?

   Currently I keep on having cause to wish for the ability to pass
   pointcuts as parameters to advice and also to store them in a list.

   Will pointcuts be modified in the future to allow these operations
   or is there some good reason for not allowing this?

   Thank You

   Piers
   _______________________________________________
   aspectj-users mailing list
   aspectj-users@xxxxxxxxxxx <mailto:aspectj-users@xxxxxxxxxxx> ------------------------------------------------------------------------


_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users


Back to the top