All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fitbur.mockito.internal.stubbing.answers.ThrowsException Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
/*
 * Copyright (c) 2007 Mockito contributors
 * This program is made available under the terms of the MIT License.
 */
package com.fitbur.mockito.internal.stubbing.answers;

import com.fitbur.mockito.internal.exceptions.stacktrace.ConditionalStackTraceFilter;
import com.fitbur.mockito.internal.util.MockUtil;
import com.fitbur.mockito.invocation.InvocationOnMock;
import com.fitbur.mockito.stubbing.Answer;

import java.io.Serializable;

public class ThrowsException implements Answer, Serializable {

    private static final long serialVersionUID = 1128820328555183980L;
    private final Throwable throwable;
    private final ConditionalStackTraceFilter filter = new ConditionalStackTraceFilter();

    public ThrowsException(Throwable throwable) {
        this.throwable = throwable;
    }

    public Object answer(InvocationOnMock invocation) throws Throwable {
        if (new MockUtil().isMock(throwable)) {
            throw throwable;
        }
        Throwable t = throwable.fillInStackTrace();
        filter.filter(t);
        throw t;
    }

    public Throwable getThrowable() {
        return throwable;
    }
}