| Re: [aspectj-users] AspectJ: help on setMockException() |
Sorry, the throws clause of an around advice only can throws exceptions that appears in the method execution captured by its join-point. In the aspectJ docs "... An advice declaration must include a throws clause listing the checked exceptions the body may throw. This list of checked exceptions must be compatible with each target join point of the advice, or an error is signalled by the compiler. ...". See Appendix B. Language Semantics -> Advices. I defined one more pointcut & the advice as you suggested:But the exception thrown by SampleComponent1.check() is not handled by this aspect.As SampleComponent1.check() is throwing NumberFormatException but not Throwable.I think crosscut is not getting generated by ajc at the required joinpoint, as thethrows clause[Throwable] of pointcut is different from throws clause [NumberFormatException]of SampleComponent1.check() method.how can I achieve generalized MockException?/** code for pointcut & advice */pointcut allCallsWithThrowsClause(): execution(* *.*(..) throws Throwable) &&
!within(com.translogicsys.aspect.ComponentTestCase);Object around() throws Throwable: allCallsWithThrowsClause()
{
String className = thisJoinPoint.getSignature().getDeclaringType().getName();
Object receiver = thisJoinPoint.getThis();
if (receiver != null)
className = receiver.getClass().getName();
String methodName = thisJoinPoint.getSignature().getName();
Object returnValue = ComponentTestCase.getMockReturnValue(className, methodName);
Throwable t=ComponentTestCase.getMockException(className,methodName);
if (returnValue == null && t!=null)
{
ComponentTestCase.indicateCalled(className, methodName, getArguments(thisJoinPoint));
throw t;
}
else
{
return proceed();
}
}/** End of code */--tnxVenkat----- Original Message -----From: Enrique J. Amodeo RubioTo: Venkat DosapatiSent: Monday, May 12, 2003 9:11 PMSubject: Re: [aspectj-users] AspectJ: help on setMockException()Venkat Dosapati wrote:
allCalls pointcut doesn't declare any throws clause. Create a new pointcut and advice:Hi Enrique,Thanx again. I did the changes whatever you adviced. I am getting this compilation errors.[Where SampleComponent is the class which uses SampleComponent1 services. Please find theattached the classes]any idea?D:\Tech\Aspectj\AspectExamples-2>ajc @mock.lst
.\SampleAspect.java:13:2: on target method-execution(public boolean SampleComponent.validate()) can't throw java.lang.Throwable
Object around() throws Throwable: allCalls()
^
.\SampleAspect.java:13:2: on target method-execution(public boolean SampleComponent.check()) can't throw java.lang.Throwable
Object around() throws Throwable: allCalls()
^
.\SampleAspect.java:13:2: on target method-execution(public String SampleComponent.getDetails()) can't throw java.lang.Throwable
Object around() throws Throwable: allCalls()
^
.\SampleAspect.java:13:2: on target method-execution(public String SampleComponent.getDetails(String)) can't throw java.lang.Throwable
Object around() throws Throwable: allCalls()
^
.\SampleAspect.java:13:2: on target method-execution(public Vector SampleComponent.testGetAll()) can't throw java.lang.Throwable
Object around() throws Throwable: allCalls()
^
.\SampleAspect.java:13:2: on target method-execution(public boolean SampleComponent1.check()) can't throw java.lang.Throwable
Object around() throws Throwable: allCalls()
^
.\SampleAspect.java:13:2: on target method-execution(public void SampleMockTestCase.testValidate()) can't throw java.lang.Throwable
Object around() throws Throwable: allCalls()
^
.\SampleAspect.java:13:2: on target method-execution(public void SampleMockTestCase.testGetDetails()) can't throw java.lang.Throwable
Object around() throws Throwable: allCalls()
^
.\SampleAspect.java:13:2: on target method-execution(public void SampleMockTestCase.testGetAll()) can't throw java.lang.Throwable
Object around() throws Throwable: allCalls()
^
.\SampleAspect.java:13:2: on target method-execution(public static void SampleMockTestCase.main(String[])) can't throw java.lang.Throwable
Object around() throws Throwable: allCalls()
^
.\SampleAspect.java:13:2: on target method-execution(private Hashtable SampleAspect.getArguments(JoinPoint)) can't throw java.lang.Throwable
Object around() throws Throwable: allCalls()
^
11 errors
--tnxVenkat----- Original Message -----From: Enrique J. Amodeo RubioSent: Monday, May 12, 2003 7:37 PMSubject: Re: [aspectj-users] AspectJ: help on setMockException()Venkat Dosapati wrote:
Here follows a quick draft of code (untested, uncompiled) only to catch the idea:hi Enrique,Thanks for your quick response. Yes, I understood the signature of the method. Buthow it should be implemented in ComponentTestCase & AspectBasedMethodInterceptor?So that I can mock the different exceptions raised by different class methods whichprovides service to the testing method.Please send me the implementation details for setMockException() if you have.Thanx in advance.--tnxVenkat----- Original Message -----From: Enrique J. Amodeo RubioSent: Monday, May 12, 2003 6:53 PMSubject: Re: [aspectj-users] AspectJ: help on setMockException()Venkat Dosapati wrote:
I downloaded the sample code and I saw that setMockException isn't implemented. I understand that setMockException indicates that the method (first parameter) must throw an exception (the second parameter)Hello everybody,I read artical on 'Virtual Mock Objects using AspectJ with JUNIT' by S.In the end of the artical I read something about setMockException(). have anyoneused setMockException(method, exception) for your junit testing?Please send me your code bits to my personal mail ID: venkat@xxxxxxxxxxxxxxxxx, asI am not able to access eclipse user groups.Thanx in advance!----
Venkat Dosapati
Enrique J. Amodeo Rubio.
In ComponentTestCase
private Map exceptions=new HashMap();
public static void setMockException(String className, String methodName,Throwable t)
{
exceptions.put(makeKey(className, methodName), t);
}
public static Object getMockException(String className, String methodName)
{
return exceptions.get(makeKey(className, methodName));
}
In AspectBasedMethodInterceptor
Object around() throws Throwable : allCalls()
{
String className = thisJoinPoint.getSignature().getDeclaringType().getName();
Object receiver = thisJoinPoint.getThis();
if (receiver != null)
className = receiver.getClass().getName();
String methodName = thisJoinPoint.getSignature().getName();
Object returnValue = ComponentTestCase.getMockReturnValue(className, methodName);
Throwable t=ComponentTestCase.getMockException(className,methodName);
if (returnValue != null)
{
ComponentTestCase.indicateCalled(className, methodName,
getArguments(thisJoinPoint));
return returnValue;
}
else if(t!=null)
{
ComponentTestCase.indicateCalled(className, methodName,
getArguments(thisJoinPoint));
throw t;
}
else
{
return proceed();
}
}
In this way you can specify that a method will throw an exception. As you can see it's incompatible with returning values.
Perhaps you should use another pointcut instead of allCalls, something called allCallsWithThrowsClause.
Hope this helps !
Enrique J. Amodeo
pointcut allCallsWithException() : execution(* *.*(..) throws Throwable) &&
!within(com.kmsoftware.test.ComponentTestCase);
and
Object around() throws Throwable : allCallsWithExceptions()
{
...
}
continue using allCalls ang its advice for methods that not throws exceptions.
Attachment:
sampleWithExceptions.zip
Description: Zip archive