1 /* 2 * Copyright (c) 2007 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 package org.mockitousage.stubbing; 6 7 import org.junit.Test; 8 import org.mockito.Mock; 9 import org.mockito.invocation.InvocationOnMock; 10 import org.mockito.stubbing.Answer; 11 import org.mockitousage.IMethods; 12 import org.mockitoutil.TestBase; 13 14 import java.lang.reflect.Method; 15 import java.util.Set; 16 17 import static org.junit.Assert.*; 18 import static org.mockito.Mockito.*; 19 20 public class StubbingWithCustomAnswerTest extends TestBase { 21 @Mock 22 private IMethods mock; 23 24 @Test shouldAnswer()25 public void shouldAnswer() throws Exception { 26 when(mock.simpleMethod(anyString())).thenAnswer(new Answer<String>() { 27 public String answer(InvocationOnMock invocation) throws Throwable { 28 String arg = invocation.getArgument(0); 29 30 return invocation.getMethod().getName() + "-" + arg; 31 } 32 }); 33 34 assertEquals("simpleMethod-test", mock.simpleMethod("test")); 35 } 36 37 @Test shouldAnswerWithThenAnswerAlias()38 public void shouldAnswerWithThenAnswerAlias() throws Exception { 39 RecordCall recordCall = new RecordCall(); 40 Set<?> mockedSet = (Set<?>) when(mock(Set.class).isEmpty()).then(recordCall).getMock(); 41 42 mockedSet.isEmpty(); 43 44 assertTrue(recordCall.isCalled()); 45 } 46 47 @Test shouldAnswerConsecutively()48 public void shouldAnswerConsecutively() throws Exception { 49 when(mock.simpleMethod()) 50 .thenAnswer(new Answer<String>() { 51 public String answer(InvocationOnMock invocation) throws Throwable { 52 return invocation.getMethod().getName(); 53 } 54 }) 55 .thenReturn("Hello") 56 .thenAnswer(new Answer<String>() { 57 public String answer(InvocationOnMock invocation) throws Throwable { 58 return invocation.getMethod().getName() + "-1"; 59 } 60 }); 61 62 assertEquals("simpleMethod", mock.simpleMethod()); 63 assertEquals("Hello", mock.simpleMethod()); 64 assertEquals("simpleMethod-1", mock.simpleMethod()); 65 assertEquals("simpleMethod-1", mock.simpleMethod()); 66 } 67 68 @Test shouldAnswerVoidMethod()69 public void shouldAnswerVoidMethod() throws Exception { 70 RecordCall recordCall = new RecordCall(); 71 72 doAnswer(recordCall).when(mock).voidMethod(); 73 74 mock.voidMethod(); 75 assertTrue(recordCall.isCalled()); 76 } 77 78 @Test shouldAnswerVoidMethodConsecutively()79 public void shouldAnswerVoidMethodConsecutively() throws Exception { 80 RecordCall call1 = new RecordCall(); 81 RecordCall call2 = new RecordCall(); 82 83 doAnswer(call1) 84 .doThrow(new UnsupportedOperationException()) 85 .doAnswer(call2) 86 .when(mock).voidMethod(); 87 88 mock.voidMethod(); 89 assertTrue(call1.isCalled()); 90 assertFalse(call2.isCalled()); 91 92 try { 93 mock.voidMethod(); 94 fail(); 95 } catch (UnsupportedOperationException e) { 96 } 97 98 mock.voidMethod(); 99 assertTrue(call2.isCalled()); 100 } 101 102 @Test shouldMakeSureTheInterfaceDoesNotChange()103 public void shouldMakeSureTheInterfaceDoesNotChange() throws Exception { 104 when(mock.simpleMethod(anyString())).thenAnswer(new Answer<String>() { 105 public String answer(InvocationOnMock invocation) throws Throwable { 106 assertTrue(invocation.getArguments().getClass().isArray()); 107 assertEquals(Method.class, invocation.getMethod().getClass()); 108 109 return "assertions passed"; 110 } 111 }); 112 113 assertEquals("assertions passed", mock.simpleMethod("test")); 114 } 115 116 private static class RecordCall implements Answer<Object> { 117 private boolean called = false; 118 isCalled()119 public boolean isCalled() { 120 return called; 121 } 122 answer(InvocationOnMock invocation)123 public Object answer(InvocationOnMock invocation) throws Throwable { 124 called = true; 125 return null; 126 } 127 } 128 129 } 130