Skip to main content

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

The simple test program above is to test the before() advice with System.out.println(...) in itself. If this is not the best method to do this kind of test, any suggestion is welcomed.


The solution you outline is the general form of a mock-object style test. You will have to do some thinking about how to expand the general technique to other problems, but I think you have the basic idea. Search the web for resources on mock object testing to see how the approach plays out in plain Java.


By the way, you mentioned "a helper class", which I could not find in Java API documentation. Could you please explain in detail what it is used for? Thank you very much.

Sorry, by "helper class" I mean an ordinary java class that implements the logic of the advice. By implementing your advice in this way you can test the behavior separate from the crosscutting. Depending on how complicated your logic is, that can be a real advantage.

Cheers,
Nick

On Jul 22, 2004, at 2:46 PM, hai lin wrote:

Hi Nick,

Thank you for your helpful advice.
I've already had a good idea suggested by Eric Bodden in [aosd-discuss]. His suggestion is to add another "test" aspect with higher precedence to advice the advices themselves, and additionally to use System.setOut() method to check the output stream. According to his suggestion, here I made a simple example to implement this kind of test:

//AspectToBeTested is the aspect to be tested
public aspect AspectToBeTested{
public pointcut pc(): set(* TestCase.i)&&within(TestCase);
before(): pc(){
System.out.println("Data"); }
}

//class TestCase provides the context for testing import java.io.*;
public class TestCase{
private int i;
public static void main(String[] args){
TestCase t=new TestCase();
t.i=3;
}
}

//SystemSetOutAspect is to redirect the output stream, with higher precedence //than AspectToBeTested import java.io.*;
public aspect SystemSetOutAspect{
public static PrintStream TestCase.console;
before(): set(* TestCase.i)&&within(TestCase){
try{
TestCase.console=System.out;
PrintStream out= new PrintStream(new BufferedOutputStream(new FileOutputStream("setOut.txt")),true); System.setOut(out);
}catch(IOException e){}
}
}

//ComparingResultAspect is to compare the output value with expected outcome, with //lower precedence than AspectToBeTested import java.io.*;
public aspect ComparingResultAspect{
before(): set(* TestCase.i)&&within(TestCase){
System.setOut(TestCase.console);
try{
BufferedReader br=new BufferedReader(new FileReader("setOut.txt"));
String in="";
while((in=br.readLine())!=null){
if(in.equals("Private")){
System.out.println("Testing is successful.");
}else{
System.out.println("Testing fails.");
}
}
}catch(IOException e){}
}
}

//PrecedenceAspect is to declare the precedence public aspect PrecedenceAspect{
declare precedence: SystemSetOutAspect, AspectToBeTested, ComparingResultAspect;
}

The simple test program above is to test the before() advice with System.out.println(...) in itself. If this is not the best method to do this kind of test, any suggestion is welcomed.

By the way, you mentioned "a helper class", which I could not find in Java API documentation. Could you please explain in detail what it is used for? Thank you very much.

Adrian

_________________________________________________________________
~{Sk~}~{A*;z5DEsSQ=xPP=;Aw#,GkJ9SC~} MSN Messenger: http://messenger.msn.com/cn
_______________________________________________
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