• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * The MIT License
3  *
4  * Copyright (c) 2016 Ian J. De Silva
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 import com.nhaarman.expect.expect
26 import org.junit.Assume.assumeTrue
27 import org.junit.Before
28 import org.junit.Test
29 import org.mockito.kotlin.*
30 import test.mockMakerInlineEnabled
31 import java.io.IOException
32 import java.math.BigInteger
33 
34 class UsingMockMakerInlineTest {
35 
36     class ClassToBeMocked {
37 
doSomethingnull38         fun doSomething(@Suppress("UNUSED_PARAMETER") c: ClassToBeMocked) {
39         }
40 
doSomethingElsenull41         fun doSomethingElse(value: BigInteger): BigInteger {
42             return value.plus(BigInteger.ONE)
43         }
44     }
45 
46     @Before
setupnull47     fun setup() {
48         mockMakerInlineEnabled = null
49         assumeTrue(mockMakerInlineEnabled())
50     }
51 
52     @Test
mockClosedClassnull53     fun mockClosedClass() {
54         /* When */
55         val result = mock<ClassToBeMocked>()
56 
57         /* Then */
58         expect(result).toNotBeNull()
59     }
60 
61     @Test
anyClosedClassnull62     fun anyClosedClass() {
63         /* Given */
64         val mock = mock<ClassToBeMocked>()
65 
66         /* When */
67         mock.doSomething(mock)
68 
69         /* Then */
70         verify(mock).doSomething(any())
71     }
72 
73     @Test
mockClosedFunction_mockStubbingnull74     fun mockClosedFunction_mockStubbing() {
75         /* Given */
76         val mock = mock<ClassToBeMocked> {
77             on { doSomethingElse(any()) } doReturn (BigInteger.ONE)
78         }
79 
80         /* When */
81         val result = mock.doSomethingElse(BigInteger.TEN)
82 
83         /* Then */
84         expect(result).toBe(BigInteger.ONE)
85     }
86 
87     @Test
mockClosedFunction_whenevernull88     fun mockClosedFunction_whenever() {
89         /* Given */
90         val mock = mock<ClassToBeMocked>()
91         whenever(mock.doSomethingElse(any())).doReturn(BigInteger.ONE)
92 
93         /* When */
94         val result = mock.doSomethingElse(BigInteger.TEN)
95 
96         /* Then */
97         expect(result).toBe(BigInteger.ONE)
98     }
99 
100     /** https://github.com/nhaarman/mockito-kotlin/issues/27 */
101     @Test
anyThrowableWithSingleThrowableConstructornull102     fun anyThrowableWithSingleThrowableConstructor() {
103         mock<Methods>().apply {
104             throwableClass(ThrowableClass(IOException()))
105             verify(this).throwableClass(any())
106         }
107     }
108 
109     interface Methods {
110 
throwableClassnull111         fun throwableClass(t: ThrowableClass)
112     }
113 
114     class ThrowableClass(cause: Throwable) : Throwable(cause)
115 
116 }
117