Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] AspectJ: help on setMockException()

Venkat Dosapati wrote:
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 the
throws 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 */
 
 
--tnx
Venkat
 
 
 
----- Original Message -----
Sent: Monday, May 12, 2003 9:11 PM
Subject: Re: [aspectj-users] AspectJ: help on setMockException()

Venkat Dosapati wrote:
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 the
attached 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
 
--tnx
Venkat
----- Original Message -----
Sent: Monday, May 12, 2003 7:37 PM
Subject: Re: [aspectj-users] AspectJ: help on setMockException()

Venkat Dosapati wrote:
hi Enrique,
 
Thanks for your quick response. Yes, I understood the signature of the method. But
how it should be implemented in ComponentTestCase & AspectBasedMethodInterceptor?
So that I can mock the different exceptions raised by different class methods which
provides service to the testing method.
 
Please send me the implementation details for setMockException() if you have.
 
Thanx in advance.
 
--tnx
Venkat
----- Original Message -----
Sent: Monday, May 12, 2003 6:53 PM
Subject: Re: [aspectj-users] AspectJ: help on setMockException()

Venkat Dosapati wrote:
Hello everybody,
 
I read artical on 'Virtual Mock Objects using AspectJ with JUNIT' by Simon Monk.
In the end of the artical I read something about setMockException(). have anyone 
used setMockException(method, exception) for your junit testing?
 
Please send me your code bits to my personal mail ID: venkat@xxxxxxxxxxxxxxxxx, as
I am not able to access eclipse user groups.
 
Thanx in advance!
 
----
Venkat Dosapati
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)

Enrique J. Amodeo Rubio.
Here follows a quick draft of code (untested, uncompiled) only to catch the idea:

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
allCalls pointcut doesn't declare any throws clause. Create a new pointcut and advice:

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.
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.

See source-code attached, for a working example. Note that you need to define an advice for each Exception you want to use.

Perhaps someones knows a more flexible way of doing all of this.

Enrique J. Amodeo Rubio

Attachment: sampleWithExceptions.zip
Description: Zip archive


Back to the top