Skip to main content

[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 Andy,

Thanks for your help, this solution started working for me partially, I was debugging to find out the call sequences, whenever there is a call to this.getFactory() the call goes to MockFactory, now over that when we call getPersistanceService()  the control flow reaches to the PS class, i thought it to be at MockPS class which i created over your code, but i doesn't worked

I created in this way:

package com.test;

public class MockFactory extends ClientFactoryFacade{
Object o;
 public MockFactory(Object o) {
   this.o = o;
 }
 
 public MockPS getPersistenceService() {// created this new method which when invoked gives an object of MockPS
 System.out.println("o="+o);
       return new MockPS();
 }
}


package com.test;

import java.util.List;

public class MockPS extends PS{
 
List query() {
   System.out.println("query called from mock");
   return null;
 }
}


public aspect X {
ClientFactoryFacade around(Object instance): call(*
SuperClass+.getFactory()) && this(instance){
return new MockFactory(instance);
}
// I was trying in this way but it didn't worked this way 
/*PS around(Object instance): call(*
SuperClass+.getPersistanceService()) && this(instance){
return new MockPS(instance);
}*/
/*ClientFactoryFacade around(Object instance): execution(*
SuperClass.getFactory()) && this(instance) {
   return new MockFactory(instance);
 }*/
}

So the problem arise with one more level of mocking, it would be great if you can give some insights on this.


Thanks,
Bhavuk

On Thu, Jun 28, 2012 at 7:27 AM, Andy Clement <andrew.clement@xxxxxxxxx> wrote:
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
>
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users



--
Thanks,
Bhavuk Soni



Back to the top