1e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinson/* 2e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinson * Copyright (c) 2007 Mockito contributors 3e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinson * This program is made available under the terms of the MIT License. 4e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinson */ 5e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinson 6e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinsonpackage org.mockito.internal.stubbing.answers; 7e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinson 8e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinsonimport org.mockito.internal.exceptions.stacktrace.ConditionalStackTraceFilter; 9e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinsonimport org.mockito.invocation.InvocationOnMock; 10e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinsonimport org.mockito.stubbing.Answer; 11e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinsonimport org.objenesis.ObjenesisHelper; 12e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinson 13e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinsonimport java.io.Serializable; 14e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinson 15e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinsonpublic class ThrowsExceptionClass implements Answer<Object>, Serializable { 16e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinson 17e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinson private Class<? extends Throwable> throwableClass; 18e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinson private final ConditionalStackTraceFilter filter = new ConditionalStackTraceFilter(); 19e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinson 20e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinson public ThrowsExceptionClass(Class<? extends Throwable> throwableClass) { 21e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinson 22e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinson this.throwableClass = throwableClass; 23e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinson } 24e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinson 25e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinson public Object answer(InvocationOnMock invocation) throws Throwable { 26e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinson 27e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinson Throwable throwable = (Throwable) ObjenesisHelper.newInstance(throwableClass); 28e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinson throwable.fillInStackTrace(); 29e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinson filter.filter(throwable); 30e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinson throw throwable; 31e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinson } 32e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinson 33e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinson public Class<? extends Throwable> getThrowableClass() { 34e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinson return throwableClass; 35e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinson } 36e0ae5d7e87b1dd6e789803c1b9615a84bd7488b7Ian Parkinson} 37