Build with Mockito 2.1
[yangtools.git] / common / testutils / src / main / java / org / opendaylight / yangtools / testutils / mockito / ThrowsMethodExceptionAnswer.java
1 /*
2  * Copyright (c) 2016 Red Hat, Inc. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.testutils.mockito;
9
10 import com.google.common.annotations.Beta;
11 import java.io.Serializable;
12 import org.mockito.Mockito;
13 import org.mockito.internal.stubbing.answers.ThrowsException;
14 import org.mockito.invocation.InvocationOnMock;
15 import org.mockito.stubbing.Answer;
16
17 /**
18  * Mockito Answer which for un-stubbed methods throws an
19  * UnstubbedMethodException (instead of Mockito's default of returning null).
20  *
21  * <p>
22  * Usage:
23  *
24  * <pre>
25  * import static ...testutils.mockito.MoreAnswers.exception;
26  *
27  * Mockito.mock(YourInterface.class, exception())
28  * </pre>
29  *
30  * @see Mockito#mock(Class, Answer)
31  *
32  * @see ThrowsException
33  *
34  * @author Michael Vorburger
35  */
36 @Beta
37 public final class ThrowsMethodExceptionAnswer implements Answer<Object>, Serializable {
38     private static final long serialVersionUID = -7316574192253912318L;
39     static final ThrowsMethodExceptionAnswer INSTANCE = new ThrowsMethodExceptionAnswer();
40
41     private ThrowsMethodExceptionAnswer() {
42
43     }
44
45     @Override
46     public Void answer(final InvocationOnMock invocation) {
47         throw new UnstubbedMethodException(invocation.getMethod());
48     }
49
50     Object readResolve() {
51         return INSTANCE;
52     }
53 }