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.misuse; 6 7 import static org.assertj.core.api.Assertions.assertThat; 8 import static org.junit.Assert.fail; 9 import static org.mockito.Mockito.*; 10 11 import org.junit.Test; 12 import org.mockito.exceptions.misusing.WrongTypeOfReturnValue; 13 14 public class SpyStubbingMisuseTest { 15 16 @Test nestedWhenTest()17 public void nestedWhenTest() { 18 Strategy mfoo = mock(Strategy.class); 19 Sampler mpoo = mock(Sampler.class); 20 Producer out = spy(new Producer(mfoo)); 21 22 try { 23 when(out.produce()).thenReturn(mpoo); 24 fail(); 25 } catch (WrongTypeOfReturnValue e) { 26 assertThat(e.getMessage()) 27 .contains("spy") 28 .contains("syntax") 29 .contains("doReturn|Throw"); 30 } 31 } 32 33 public class Sample {} 34 35 public class Strategy { getSample()36 Sample getSample() { 37 return new Sample(); 38 } 39 } 40 41 public class Sampler { 42 Sample sample; 43 Sampler(Strategy f)44 Sampler(Strategy f) { 45 sample = f.getSample(); 46 } 47 } 48 49 public class Producer { 50 Strategy strategy; 51 Producer(Strategy f)52 Producer(Strategy f) { 53 strategy = f; 54 } 55 produce()56 Sampler produce() { 57 return new Sampler(strategy); 58 } 59 } 60 } 61