1/* 2 * Copyright (c) 2007 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5package org.mockito.internal.stubbing.answers; 6 7import org.mockito.internal.exceptions.stacktrace.ConditionalStackTraceFilter; 8import org.mockito.internal.util.MockUtil; 9import org.mockito.invocation.InvocationOnMock; 10import org.mockito.stubbing.Answer; 11 12import java.io.Serializable; 13 14public class ThrowsException implements Answer<Object>, Serializable { 15 16 private static final long serialVersionUID = 1128820328555183980L; 17 private final Throwable throwable; 18 private final ConditionalStackTraceFilter filter = new ConditionalStackTraceFilter(); 19 20 public ThrowsException(Throwable throwable) { 21 this.throwable = throwable; 22 } 23 24 public Object answer(InvocationOnMock invocation) throws Throwable { 25 if (new MockUtil().isMock(throwable)) { 26 throw throwable; 27 } 28 Throwable t = throwable.fillInStackTrace(); 29 filter.filter(t); 30 throw t; 31 } 32 33 public Throwable getThrowable() { 34 return throwable; 35 } 36}