-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathMockitoSugar.scala
156 lines (148 loc) · 5.03 KB
/
MockitoSugar.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* Copyright 2001-2013 Artima, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.scalatestplus.mockito
import org.mockito.Mockito.{mock => mockitoMock}
import reflect.ClassTag
import org.mockito.stubbing.Answer
import org.mockito.MockSettings
/**
* Trait that provides some basic syntax sugar for <a href="http://mockito.org/" target="_blank">Mockito</a>.
*
* <p>
* Using the Mockito API directly, you create a mock with:
* </p>
*
* <pre class="stHighlight">
* val mockCollaborator = mock(classOf[Collaborator])
* </pre>
*
* <p>
* Using this trait, you can shorten that to:
* </p>
*
* <pre class="stHighlight">
* val mockCollaborator = mock[Collaborator]
* </pre>
*
* <p>
* This trait also provides shorthands for the three other (non-deprecated) overloaded <code>mock</code> methods,
* which allow you to pass in a default answer, a name, or settings.
* </p>
*
* @author Bill Venners
* @author Chua Chee Seng
*/
trait MockitoSugar {
/**
* Invokes the <code>mock(classToMock: Class[T])</code> method on the <code>Mockito</code> companion object (<em>i.e.</em>, the
* static <code>mock(java.lang.Class<T> classToMock)</code> method in Java class <code>org.mockito.Mockito</code>).
*
* <p>
* Using the Mockito API directly, you create a mock with:
* </p>
*
* <pre class="stHighlight">
* val mockCollaborator = mock(classOf[Collaborator])
* </pre>
*
* <p>
* Using this method, you can shorten that to:
* </p>
*
* <pre class="stHighlight">
* val mockCollaborator = mock[Collaborator]
* </pre>
*/
def mock[T <: AnyRef](implicit classTag: ClassTag[T]): T = {
mockitoMock(classTag.runtimeClass.asInstanceOf[Class[T]])
}
/**
* Invokes the <code>mock(classToMock: Class[T], defaultAnswer: Answer[_])</code> method on the <code>Mockito</code> companion object (<em>i.e.</em>, the
* static <code>mock(java.lang.Class<T> classToMock, org.mockito.stubbing.Answer defaultAnswer)</code> method in Java class <code>org.mockito.Mockito</code>).
*
* <p>
* Using the Mockito API directly, you create a mock with:
* </p>
*
* <pre class="stHighlight">
* val mockCollaborator = mock(classOf[Collaborator], defaultAnswer)
* </pre>
*
* <p>
* Using this method, you can shorten that to:
* </p>
*
* <pre class="stHighlight">
* val mockCollaborator = mock[Collaborator](defaultAnswer)
* </pre>
*/
def mock[T <: AnyRef](defaultAnswer: Answer[_])(implicit classTag: ClassTag[T]): T = {
mockitoMock(classTag.runtimeClass.asInstanceOf[Class[T]], defaultAnswer)
}
/**
* Invokes the <code>mock(classToMock: Class[T], mockSettings: MockSettings)</code> method on the <code>Mockito</code> companion object (<em>i.e.</em>, the
* static <code>mock(java.lang.Class<T> classToMock, org.mockito.MockSettings mockSettings)</code> method in Java class <code>org.mockito.Mockito</code>).
*
* <p>
* Using the Mockito API directly, you create a mock with:
* </p>
*
* <pre class="stHighlight">
* val mockCollaborator = mock(classOf[Collaborator], mockSettings)
* </pre>
*
* <p>
* Using this method, you can shorten that to:
* </p>
*
* <pre class="stHighlight">
* val mockCollaborator = mock[Collaborator](mockSettings)
* </pre>
*/
def mock[T <: AnyRef](mockSettings: MockSettings)(implicit classTag: ClassTag[T]): T = {
mockitoMock(classTag.runtimeClass.asInstanceOf[Class[T]], mockSettings)
}
/**
* Invokes the <code>mock(classToMock: Class[T], name: String)</code> method on the <code>Mockito</code> companion object (<em>i.e.</em>, the
* static <code>mock(java.lang.Class<T> classToMock, java.lang.String name)</code> method in Java class <code>org.mockito.Mockito</code>).
*
* <p>
* Using the Mockito API directly, you create a mock with:
* </p>
*
* <pre class="stHighlight">
* val mockCollaborator = mock(classOf[Collaborator], name)
* </pre>
*
* <p>
* Using this method, you can shorten that to:
* </p>
*
* <pre class="stHighlight">
* val mockCollaborator = mock[Collaborator](name)
* </pre>
*/
def mock[T <: AnyRef](name: String)(implicit classTag: ClassTag[T]): T = {
mockitoMock(classTag.runtimeClass.asInstanceOf[Class[T]], name)
}
}
/**
* Companion object that facilitates the importing of <code>MockitoSugar</code> members as
* an alternative to mixing it in. One use case is to import <code>MockitoSugar</code> members so you can use
* them in the Scala interpreter.
*/
// TODO: Fill in an example
object MockitoSugar extends MockitoSugar