• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download

<lambda>null1 package test
2 
3 import com.nhaarman.expect.expect
4 import org.junit.Test
5 import org.mockito.kotlin.*
6 import org.mockito.stubbing.Answer
7 
8 class BDDMockitoTest {
9 
10     @Test
11     fun given_will_properlyStubs() {
12         /* Given */
13         val mock = mock<Methods>()
14 
15         /* When */
16         given(mock.stringResult()) will Answer<String> { "Test" }
17 
18         /* Then */
19         expect(mock.stringResult()).toBe("Test")
20     }
21 
22     @Test
23     fun given_willReturn_properlyStubs() {
24         /* Given */
25         val mock = mock<Methods>()
26 
27         /* When */
28         given(mock.stringResult()).willReturn("Test")
29 
30         /* Then */
31         expect(mock.stringResult()).toBe("Test")
32     }
33 
34     @Test
35     fun givenLambda_willReturn_properlyStubs() {
36         /* Given */
37         val mock = mock<Methods>()
38 
39         /* When */
40         given { mock.stringResult() }.willReturn("Test")
41 
42         /* Then */
43         expect(mock.stringResult()).toBe("Test")
44     }
45 
46     @Test
47     fun given_willReturnLambda_properlyStubs() {
48         /* Given */
49         val mock = mock<Methods>()
50 
51         /* When */
52         given(mock.stringResult()).willReturn { "Test" }
53 
54         /* Then */
55         expect(mock.stringResult()).toBe("Test")
56     }
57 
58     @Test
59     fun givenLambda_willReturnLambda_properlyStubs() {
60         /* Given */
61         val mock = mock<Methods>()
62 
63         /* When */
64         given { mock.stringResult() } willReturn { "Test" }
65 
66         /* Then */
67         expect(mock.stringResult()).toBe("Test")
68     }
69 
70     @Test
71     fun given_willAnswer_properlyStubs() {
72         /* Given */
73         val mock = mock<Methods>()
74 
75         /* When */
76         given(mock.stringResult()).willAnswer { "Test" }
77 
78         /* Then */
79         expect(mock.stringResult()).toBe("Test")
80     }
81 
82     @Test
83     fun given_willAnswerInfix_properlyStubs() {
84         /* Given */
85         val mock = mock<Methods>()
86 
87         /* When */
88         given(mock.stringResult()) willAnswer { "Test" }
89 
90         /* Then */
91         expect(mock.stringResult()).toBe("Test")
92     }
93 
94     @Test
95     fun given_willAnswerInfix_withInvocationInfo_properlyStubs() {
96         /* Given */
97         val mock = mock<Methods>()
98 
99         /* When */
100         given(mock.stringResult(any())) willAnswer { invocation ->
101             (invocation.arguments[0] as String)
102                 .reversed()
103         }
104 
105         /* Then */
106         expect(mock.stringResult("Test")).toBe("tseT")
107     }
108 
109     @Test(expected = IllegalStateException::class)
110     fun given_willThrowInfix_properlyStubs() {
111         /* Given */
112         val mock = mock<Methods>()
113 
114         /* When */
115         given(mock.stringResult()) willThrow { IllegalStateException() }
116         mock.stringResult()
117     }
118 
119     @Test
120     fun then() {
121         /* Given */
122         val mock = mock<Methods>()
123         whenever(mock.stringResult()).thenReturn("Test")
124 
125         /* When */
126         mock.stringResult()
127 
128         /* Then */
129         org.mockito.kotlin.then(mock).should().stringResult()
130     }
131 }
132