|
16 | 16 | package org.spockframework.smoke.mock |
17 | 17 |
|
18 | 18 | import org.spockframework.mock.CannotCreateMockException |
| 19 | +import org.spockframework.runtime.GroovyRuntimeUtil |
19 | 20 | import org.spockframework.runtime.model.parallel.Resources |
| 21 | +import spock.lang.Issue |
| 22 | +import spock.lang.Requires |
20 | 23 | import spock.lang.ResourceLock |
21 | 24 | import spock.lang.Specification |
22 | 25 |
|
@@ -126,11 +129,160 @@ class GroovyMocksForInterfaces extends Specification { |
126 | 129 | ex.message == "Cannot create mock for interface java.lang.Runnable. Global mocking is only possible for classes, but not for interfaces." |
127 | 130 | } |
128 | 131 |
|
| 132 | + @Issue("https://github.com/spockframework/spock/issues/2131") |
| 133 | + def "Boolean is getters shall be mockable"() { |
| 134 | + expect: |
| 135 | + m.booleanValue |
| 136 | + m.isBooleanValue() |
| 137 | + |
| 138 | + where: |
| 139 | + m << [ |
| 140 | + Stub(BooleanGetters) { |
| 141 | + booleanValue >> true |
| 142 | + }, |
| 143 | + Stub(BooleanGetters) { |
| 144 | + isBooleanValue() >> true |
| 145 | + } |
| 146 | + ] |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Verify that a boxed Boolean is getter does not have a property representation in Groovy >= 4. |
| 151 | + */ |
| 152 | + @Requires({ GroovyRuntimeUtil.MAJOR_VERSION >= 4 }) |
| 153 | + def "Boolean boxed is getters Groovy >=4 semantic check"() { |
| 154 | + given: |
| 155 | + def m = createBooleanGetterInterfaceObjPlain() |
| 156 | + |
| 157 | + expect: |
| 158 | + m.isBooleanValueBoxed() |
| 159 | + |
| 160 | + when: |
| 161 | + m.booleanValueBoxed |
| 162 | + then: |
| 163 | + thrown(MissingPropertyException) |
| 164 | + //The property for a boxed Boolean does not exist in Groovy >=4 |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Verify that a boxed Boolean is getter does have a property representation in Groovy <= 3. |
| 169 | + */ |
| 170 | + @Requires({ GroovyRuntimeUtil.MAJOR_VERSION <= 3 }) |
| 171 | + def "Boolean boxed is getters Groovy <= 3 semantic check"() { |
| 172 | + given: |
| 173 | + def m = createBooleanGetterInterfaceObjPlain() |
| 174 | + |
| 175 | + expect: |
| 176 | + m.booleanValueBoxed |
| 177 | + m.isBooleanValueBoxed() |
| 178 | + } |
| 179 | + |
| 180 | + @Issue("https://github.com/spockframework/spock/issues/2131") |
| 181 | + @Requires({ GroovyRuntimeUtil.MAJOR_VERSION >= 4 }) |
| 182 | + def "Boolean boxed is getters is not mockable Groovy >= 4"() { |
| 183 | + given: |
| 184 | + def m = Stub(BooleanGetters) { |
| 185 | + isBooleanValueBoxed() >> true |
| 186 | + } |
| 187 | + |
| 188 | + expect: |
| 189 | + m.isBooleanValueBoxed() |
| 190 | + |
| 191 | + when: |
| 192 | + m.booleanValueBoxed |
| 193 | + then: |
| 194 | + thrown(MissingPropertyException) |
| 195 | + //According to Groovy >=4 rules the property for a boxed Boolean is getter does not exist. |
| 196 | + } |
| 197 | + |
| 198 | + @Issue("https://github.com/spockframework/spock/issues/2131") |
| 199 | + @Requires({ GroovyRuntimeUtil.MAJOR_VERSION <= 3 }) |
| 200 | + def "Boolean boxed is getters shall be mockable Groovy <= 3"() { |
| 201 | + expect: |
| 202 | + //Groovy <= 3 does not throw an MissingProperty exception |
| 203 | + m.booleanValueBoxed |
| 204 | + m.isBooleanValueBoxed() |
| 205 | + |
| 206 | + where: |
| 207 | + m << [ |
| 208 | + Stub(BooleanGetters) { |
| 209 | + booleanValueBoxed >> true |
| 210 | + }, |
| 211 | + Stub(BooleanGetters) { |
| 212 | + isBooleanValueBoxed() >> true |
| 213 | + } |
| 214 | + ] |
| 215 | + } |
| 216 | + |
| 217 | + @Issue("https://github.com/spockframework/spock/issues/2131") |
| 218 | + def "Boolean get getters shall be mockable"() { |
| 219 | + expect: |
| 220 | + m.isBooleanValue |
| 221 | + m.getIsBooleanValue() |
| 222 | + |
| 223 | + where: |
| 224 | + m << [ |
| 225 | + Stub(BooleanGetters) { |
| 226 | + isBooleanValue >> true |
| 227 | + }, |
| 228 | + Stub(BooleanGetters) { |
| 229 | + getIsBooleanValue() >> true |
| 230 | + } |
| 231 | + ] |
| 232 | + } |
| 233 | + |
| 234 | + @Issue("https://github.com/spockframework/spock/issues/2131") |
| 235 | + def "Boolean boxed get getters shall be mockable"() { |
| 236 | + expect: |
| 237 | + m.isBooleanValueBoxed |
| 238 | + m.getIsBooleanValueBoxed() |
| 239 | + |
| 240 | + where: |
| 241 | + m << [ |
| 242 | + Stub(BooleanGetters) { |
| 243 | + isBooleanValueBoxed >> true |
| 244 | + }, |
| 245 | + Stub(BooleanGetters) { |
| 246 | + getIsBooleanValueBoxed() >> true |
| 247 | + } |
| 248 | + ] |
| 249 | + } |
| 250 | + |
129 | 251 | interface Person { |
130 | 252 | String getName() |
131 | 253 |
|
132 | 254 | void setName(String name) |
133 | 255 |
|
134 | 256 | void sing(String song) |
135 | 257 | } |
| 258 | + |
| 259 | + interface BooleanGetters { |
| 260 | + boolean isBooleanValue() |
| 261 | + |
| 262 | + Boolean isBooleanValueBoxed() |
| 263 | + |
| 264 | + boolean getIsBooleanValue() |
| 265 | + |
| 266 | + Boolean getIsBooleanValueBoxed() |
| 267 | + } |
| 268 | + |
| 269 | + private BooleanGetters createBooleanGetterInterfaceObjPlain() { |
| 270 | + new BooleanGetters() { |
| 271 | + boolean isBooleanValue() { |
| 272 | + true |
| 273 | + } |
| 274 | + |
| 275 | + Boolean isBooleanValueBoxed() { |
| 276 | + true |
| 277 | + } |
| 278 | + |
| 279 | + boolean getIsBooleanValue() { |
| 280 | + true |
| 281 | + } |
| 282 | + |
| 283 | + Boolean getIsBooleanValueBoxed() { |
| 284 | + true |
| 285 | + } |
| 286 | + } |
| 287 | + } |
136 | 288 | } |
0 commit comments