[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Injecting dependency of superclass into subclass method using aspectJ

Hi,

Not sure I'm 100% understanding your scenario, but this code
intercepts the call to getFactory() and returns something else:

import java.util.*;

class Superclass {
  ClientFactoryFacade factoryFacade;
  public ClientFactoryFacade getFactory() {
	return factoryFacade;
  }
}

class PS {
  List query() {
    System.out.println("query called");
    return null;
  }
}

class ClientFactoryFacade {
  public PS getPersistenceService() {
	return null;
  }
}

public class Subclass extends Superclass {
  public static void main(String []argv) {
	Subclass sc = new Subclass();
	sc.methodUnderTest();
  }
  public void methodUnderTest() {
    // want to remove this dependency with aspectj how to get hold of
this.getfactory()
    Collection col = this.getFactory().getPersistenceService().query();
  }
}

aspect X {
  ClientFactoryFacade around(Object instance): call(*
Superclass+.getFactory()) && this(instance) {
    return new MockFactory(instance);
  }
}

class MockFactory extends ClientFactoryFacade {
  Object o;
  MockFactory(Object o) {
    this.o = o;
  }
  public PS getPersistenceService() {
	System.out.println("o="+o);
	return new PS();
  }
}

An alternative would be to intercept the execution of the superclass
method instead of calls to it and return the mock then:

ClientFactoryFacade around(Object instance): execution(*
Superclass.getFactory()) && this(instance) {
    return new MockFactory(instance);
  }

If that isn't helpful - if you give me a bit more of an explanation of
what you want, I can see what I can do.

cheers,
Andy

On 27 June 2012 09:47, Bhavuk soni <bhavuksoni@xxxxxxxxx> wrote:
> Hi,
>
> I am working on a scenario where i have to test a method in a class, whose
> dependency is with the super class.
>
> For Example:
>
> public SuperClass{
>
> Public ClientFactoryFacade getFactory(){
>
>      return factoryFacade;// Some code that returns the factory object
>    }
>
> }
>
>
> Public SubClass extends SuperClass{
>
>     Public void methodUnderTest(){
>
>         Collection col = this.getFactory().getPersistenceService().query();
> // want to remove this dependency with aspectJ, how to get hold of
> this.getFactory()
>     }
> }
>
>
> I have mock class ready for SuperClass, but struggling for injecting it
> whenever the test method is called in the subclass, Please suggest on this,
> happy if some code is there to understand.
> --
> Thanks,
> Bhavuk Soni
>
>
>
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
>