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

Back to the top