Abhijit
Abhijit

Reputation: 63777

How to throw exception with super constructor been mocked

I have an exception class

public class FooStudioException extends SpamException {

    public FooStudioException(String message, String localizedMessage, Throwable cause) {
        super(SpamErrorCodes.INTERNAL_ERROR, message, localizedMessage, cause);
    }

    public FooStudioException(String message, String localizedMessage) {
        super(SpamErrorCodes.INTERNAL_ERROR, message, localizedMessage);
    }
}
public class SpamException extends Exception {
    .....
    public SpamException(SpamErrorCodes codes, String message, String localizedMessage, Throwable cause) {
        super(message, cause);
        .....
    }

which is been used inside a method

public class FooInstrumentAspect extends InstrumentAspect {

    @Pointcut("@annotation(Foo.aop.annotation.FooInstrument)")
    public void FooInstrumentPointcut() {}

    @Around("FooInstrumentPointcut()")
    public Object aroundFooInstrumented(ProceedingJoinPoint pjp) throws Throwable {
        try {
            return aroundInstrumented(pjp);
        } catch (FooStudioException ex) {
            throw ex.getCause();
        }
    }
}
@Aspect
public class InstrumentAspect {

........
     */
    @Around("instrumentPointcut()")
    public Object aroundInstrumented(ProceedingJoinPoint pjp) throws Throwable {



        Object result = null;
        Throwable throwable = null;

        try {
            result = pjp.proceed();
        } catch (Throwable t) {
            throwable = t;
        }
        if (throwable != null) {
            throw throwable;
        }

        return result;
    }

I would like to throw the exception FooStudioException when pjp.proceed is called. But I would also want to ensure when FooStudioException is been thrown, constructor of SpamException is mocked.

How can I achieve it?

I am ready to use powermock if required.

Upvotes: 0

Views: 75

Answers (0)

Related Questions