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.Mockito; 8 import org.mockito.NotExtensible; 9 10 /** 11 * Base interface for stubbing consecutive method calls with {@link Mockito#doReturn(Object)} syntax. 12 * This interface is needed so that we can reuse the same hierarchy in subinterfaces. 13 * 14 * @since 2.20.0 15 */ 16 @NotExtensible 17 public interface BaseStubber { 18 19 /** 20 * Use it for stubbing consecutive calls in {@link Mockito#doThrow(Throwable[])} style: 21 * <pre class="code"><code class="java"> 22 * doThrow(new RuntimeException("one")). 23 * doThrow(new RuntimeException("two")) 24 * .when(mock).someVoidMethod(); 25 * </code></pre> 26 * See javadoc for {@link Mockito#doThrow(Throwable[])} 27 * 28 * @param toBeThrown to be thrown when the stubbed method is called 29 * @return stubber - to select a method for stubbing 30 */ doThrow(Throwable... toBeThrown)31 Stubber doThrow(Throwable... toBeThrown); 32 33 /** 34 * Use it for stubbing consecutive calls in {@link Mockito#doThrow(Class)} style: 35 * <pre class="code"><code class="java"> 36 * doThrow(RuntimeException.class). 37 * doThrow(IllegalArgumentException.class) 38 * .when(mock).someVoidMethod(); 39 * </code></pre> 40 * See javadoc for {@link Mockito#doThrow(Class)} 41 * 42 * @param toBeThrown exception class to be thrown when the stubbed method is called 43 * @return stubber - to select a method for stubbing 44 * 45 * @since 2.1.0 46 */ doThrow(Class<? extends Throwable> toBeThrown)47 Stubber doThrow(Class<? extends Throwable> toBeThrown); 48 49 /** 50 * Use it for stubbing consecutive calls in {@link Mockito#doThrow(Class)} style: 51 * <pre class="code"><code class="java"> 52 * doThrow(RuntimeException.class). 53 * doThrow(IllegalArgumentException.class) 54 * .when(mock).someVoidMethod(); 55 * </code></pre> 56 * See javadoc for {@link Mockito#doThrow(Class)} 57 * 58 * @param toBeThrown exception class to be thrown when the stubbed method is called 59 * @param nextToBeThrown exception class next to be thrown when the stubbed method is called 60 * @return stubber - to select a method for stubbing 61 * 62 * @since 2.1.0 63 */ 64 // Additional method helps users of JDK7+ to hide heap pollution / unchecked generics array creation 65 @SuppressWarnings ({"unchecked", "varargs"}) doThrow(Class<? extends Throwable> toBeThrown, Class<? extends Throwable>... nextToBeThrown)66 Stubber doThrow(Class<? extends Throwable> toBeThrown, Class<? extends Throwable>... nextToBeThrown); 67 68 /** 69 * Use it for stubbing consecutive calls in {@link Mockito#doAnswer(Answer)} style: 70 * <pre class="code"><code class="java"> 71 * doAnswer(answerOne). 72 * doAnswer(answerTwo) 73 * .when(mock).someVoidMethod(); 74 * </code></pre> 75 * See javadoc for {@link Mockito#doAnswer(Answer)} 76 * 77 * @param answer to answer when the stubbed method is called 78 * @return stubber - to select a method for stubbing 79 */ doAnswer(Answer answer)80 Stubber doAnswer(Answer answer); 81 82 /** 83 * Use it for stubbing consecutive calls in {@link Mockito#doNothing()} style: 84 * <pre class="code"><code class="java"> 85 * doNothing(). 86 * doThrow(new RuntimeException("two")) 87 * .when(mock).someVoidMethod(); 88 * </code></pre> 89 * See javadoc for {@link Mockito#doNothing()} 90 * 91 * @return stubber - to select a method for stubbing 92 */ doNothing()93 Stubber doNothing(); 94 95 /** 96 * Use it for stubbing consecutive calls in {@link Mockito#doReturn(Object)} style. 97 * <p> 98 * See javadoc for {@link Mockito#doReturn(Object)} 99 * 100 * @param toBeReturned to be returned when the stubbed method is called 101 * @return stubber - to select a method for stubbing 102 */ doReturn(Object toBeReturned)103 Stubber doReturn(Object toBeReturned); 104 105 /** 106 * Use it for stubbing consecutive calls in {@link Mockito#doReturn(Object)} style. 107 * <p> 108 * See javadoc for {@link Mockito#doReturn(Object, Object...)} 109 * 110 * @param toBeReturned to be returned when the stubbed method is called 111 * @param nextToBeReturned to be returned in consecutive calls when the stubbed method is called 112 * @return stubber - to select a method for stubbing 113 */ 114 @SuppressWarnings({"unchecked", "varargs"}) doReturn(Object toBeReturned, Object... nextToBeReturned)115 Stubber doReturn(Object toBeReturned, Object... nextToBeReturned); 116 117 /** 118 * Use it for stubbing consecutive calls in {@link Mockito#doCallRealMethod()} style. 119 * <p> 120 * See javadoc for {@link Mockito#doCallRealMethod()} 121 * 122 * @return stubber - to select a method for stubbing 123 */ doCallRealMethod()124 Stubber doCallRealMethod(); 125 } 126