-
Notifications
You must be signed in to change notification settings - Fork 468
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add check to prevent double global mocking (#1792)
Previous to this commit, it was possible to create a global GroovyMock/Stub/Spy for a type that was already a global mock, this is problematic and leaked the modified meta-class.
- Loading branch information
Showing
2 changed files
with
39 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
spock-specs/src/test/groovy/org/spockframework/smoke/mock/GroovyMocks.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,45 @@ | ||
package org.spockframework.smoke.mock | ||
|
||
import org.spockframework.mock.CannotCreateMockException | ||
import spock.lang.Specification | ||
import spock.lang.Unroll | ||
|
||
class GroovyMocks extends Specification { | ||
def "implement GroovyObject"() { | ||
expect: | ||
GroovyMock(List) instanceof GroovyObject | ||
GroovyMock(ArrayList) instanceof GroovyObject | ||
} | ||
|
||
@Unroll("A Groovy#typeB can't be created for a type that was already Groovy#typeA'd") | ||
def "global GroovyMocks can't be created for a type that is already mocked"(String typeA, String typeB) { | ||
given: | ||
createMock(typeA) | ||
|
||
when: | ||
createMock(typeB) | ||
|
||
then: | ||
CannotCreateMockException e = thrown() | ||
e.message == 'Cannot create mock for class java.util.ArrayList. The given type is already mocked by Spock.' | ||
|
||
where: | ||
[typeA, typeB] << ([['Mock', 'Stub', 'Spy']] * 2).combinations() | ||
} | ||
|
||
void createMock(String type) { | ||
switch (type) { | ||
case 'Mock': | ||
GroovyMock(global: true, ArrayList) | ||
break | ||
case 'Stub': | ||
GroovyStub(global: true, ArrayList) | ||
break | ||
case 'Spy': | ||
GroovySpy(global: true, ArrayList) | ||
break | ||
default: | ||
throw new IllegalArgumentException(type) | ||
} | ||
} | ||
} |