Home | History | Annotate | Download | only in stubbing
      1 /*
      2  * Copyright (c) 2007 Mockito contributors
      3  * This program is made available under the terms of the MIT License.
      4  */
      5 package org.mockito.stubbing;
      6 
      7 import org.mockito.invocation.InvocationOnMock;
      8 
      9 /**
     10  * Generic interface to be used for configuring mock's answer.
     11  * Answer specifies an action that is executed and a return value that is returned when you interact with the mock.
     12  * <p>
     13  * Example of stubbing a mock with custom answer:
     14  *
     15  * <pre class="code"><code class="java">
     16  * when(mock.someMethod(anyString())).thenAnswer(new Answer() {
     17  *     Object answer(InvocationOnMock invocation) {
     18  *         Object[] args = invocation.getArguments();
     19  *         Object mock = invocation.getMock();
     20  *         return "called with arguments: " + args;
     21  *     }
     22  * });
     23  *
     24  * //Following prints "called with arguments: foo"
     25  * System.out.println(mock.someMethod("foo"));
     26  * </code></pre>
     27  *
     28  * @param <T> the type to return.
     29  */
     30 public interface Answer<T> {
     31     /**
     32      * @param invocation the invocation on the mock.
     33      *
     34      * @return the value to be returned
     35      *
     36      * @throws Throwable the throwable to be thrown
     37      */
     38     T answer(InvocationOnMock invocation) throws Throwable;
     39 }