• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 org.junit.Test;
8 import org.mockito.exceptions.misusing.WrongTypeOfReturnValue;
9 
10 import static org.assertj.core.api.Assertions.assertThat;
11 import static org.junit.Assert.fail;
12 import static org.mockito.Mockito.*;
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()).contains("spy").contains("syntax").contains("doReturn|Throw");
27         }
28     }
29 
30     public class Sample { }
31 
32     public class Strategy {
getSample()33         Sample getSample() {
34             return new Sample();
35         }
36     }
37 
38     public class Sampler {
39         Sample sample;
Sampler(Strategy f)40         Sampler(Strategy f) {
41             sample = f.getSample();
42         }
43     }
44 
45     public class Producer {
46         Strategy strategy;
Producer(Strategy f)47         Producer(Strategy f) {
48             strategy = f;
49         }
produce()50         Sampler produce() {
51             return new Sampler(strategy);
52         }
53     }
54 }
55