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.internal.stubbing; 6 7 import org.mockito.exceptions.Reporter; 8 import org.mockito.internal.stubbing.answers.*; 9 import org.mockito.internal.util.MockUtil; 10 import org.mockito.stubbing.Answer; 11 import org.mockito.stubbing.Stubber; 12 13 import java.util.LinkedList; 14 import java.util.List; 15 16 @SuppressWarnings("unchecked") 17 public class StubberImpl implements Stubber { 18 19 final List<Answer> answers = new LinkedList<Answer>(); 20 private final Reporter reporter = new Reporter(); 21 when(T mock)22 public <T> T when(T mock) { 23 MockUtil mockUtil = new MockUtil(); 24 25 if (mock == null) { 26 reporter.nullPassedToWhenMethod(); 27 } else { 28 if (!mockUtil.isMock(mock)) { 29 reporter.notAMockPassedToWhenMethod(); 30 } 31 } 32 33 mockUtil.getMockHandler(mock).setAnswersForStubbing(answers); 34 return mock; 35 } 36 doReturn(Object toBeReturned)37 public Stubber doReturn(Object toBeReturned) { 38 answers.add(new Returns(toBeReturned)); 39 return this; 40 } 41 doThrow(Throwable toBeThrown)42 public Stubber doThrow(Throwable toBeThrown) { 43 answers.add(new ThrowsException(toBeThrown)); 44 return this; 45 } 46 doThrow(Class<? extends Throwable> toBeThrown)47 public Stubber doThrow(Class<? extends Throwable> toBeThrown) { 48 answers.add(new ThrowsExceptionClass(toBeThrown)); 49 return this; 50 } 51 doNothing()52 public Stubber doNothing() { 53 answers.add(new DoesNothing()); 54 return this; 55 } 56 doAnswer(Answer answer)57 public Stubber doAnswer(Answer answer) { 58 answers.add(answer); 59 return this; 60 } 61 doCallRealMethod()62 public Stubber doCallRealMethod() { 63 answers.add(new CallsRealMethods()); 64 return this; 65 } 66 } 67