[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
|
- From: Andy Clement <andrew.clement@xxxxxxxxx>
- Date: Wed, 27 Jun 2012 18:57:21 -0700
- Delivered-to: aspectj-users@eclipse.org
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; bh=BuftGRVuGN4QSSuT0YsvBX1n4AyO8emx60Fu41Ldi+Y=; b=qEbrV2tJs3+A23QJFPN5/vlpH8ug3NT8vQ0/4/rJf3Or4Ca8hcu5RWcjJB7Tyy2e9h TLXjlCb4alcmjkifo/OsagTuRepPo4tX9CsCQfyrBEvK+eR9vUszguo0jtxM2Bp1yLVQ jf9x38Xb9HgR7reeyJ6FxCuMmfNsP+zMkQN3+NvGAN7TyOVzoj5VE+e8NrYrSQWKSSe2 bcD7p8/ehuqyYDSOqOw1Y3aqpjdaKu4gPNMV5p39d30lUH4FR+JPflyu+A8Khlzp1WJQ eUGCwNkNNWLhDI0UBdKQgbQMW4QHj7eSQ7QkC5akAee3GBij+ez4Oujaz8yKA+rzcbhe Z4xQ==
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
>