Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Testing Aspect Alone

I typically test aspects "by example". Your case is a bit difficult, since it involves a static call, but the following aspect:

public aspect ContractEnforcement {
pointcut add(int quantity)
: call(* Cart.add(..)) && args(quantity, ..);

before(int quantity) : add(quantity) {
if(quantity < 1) throw new IllegalArgumentException();
}
}

can be tested like this:

public void testNegativeAddThrowsException(){
Cart cart = new Cart();
try{
cart.add(-1, new Book());
fail("Expecter IAE from aspect.");
}
catch(IllegalArgumentException e){
//success!
}
}

Testing by example encourages the use of interfaces in the definition of pointcuts, since it's easy to implement an interface on a test stub and have the aspect apply to it.

For examples such as yours, where the advice creates what amounts to a side effect, you could consider using a cflow() based pointcut to catch calls to the code that implements the side effect. If the side effect itself is difficult or complex, I'd consider refactoring the aspect to use a helper class that could be tested in isolation.

before(SomeObject someObject): somePointCut(someObject){
new Helper(someObject).doSomething();
}

Just some thoughts,
Nick


On Jul 20, 2004, at 4:05 AM, lin hai wrote:

I'm doing my project to develop a test tool for aspect-oriented programs. Now I am still at the stage of how to program to test the aspect alone and have some difficulties on this stage.

I have been thinking of how to test before(), after() and void around() advice which have no return value, which means no comparison can be made if there is no return value and hence no expected value for these advices as mentioned above. I've tried many ways to help implement the testing. One way is to add some extra code in advice to recognize the control flow, but adding code is really bad idea in testing field. The other way is to compare the resulting value of variables or invoked methods used in advice with expected value, but these variables or invoked methods with return value do not always happen in advice, for example, how to test the following simple before() advice:

public aspect MannersAspect {
pointcut deliverMessage()
: call(* MessageCommunicator.deliver(..));

before() : deliverMessage() {
System.out.print("Hello! ");
}
}

Does anyone have any other ways to test these advice? Any suggestions are welcome.

Adrian

_________________________________________________________________
享用世界上最大的电子邮件系统― MSN Hotmail http://www.hotmail.com
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
http://dev.eclipse.org/mailman/listinfo/aspectj-users


Nicholas Lesiecki
Software Craftsman, specializing in J2EE,
Agile Methods, and aspect-oriented programming

Books:
* Mastering AspectJ: http://tinyurl.com/66vf
* Java Tools for Extreme Programming: http://tinyurl.com/66vt

Articles on AspectJ:
* http://tinyurl.com/66vu and http://tinyurl.com/66vv

Back to the top