-
Notifications
You must be signed in to change notification settings - Fork 469
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make Groovy global mocking more strict (#1848)
Add check to prevent double global mocking Co-authored-by: Andreas Turban <[email protected]>
- Loading branch information
Showing
7 changed files
with
102 additions
and
3 deletions.
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
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
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
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
3 changes: 3 additions & 0 deletions
3
spock-specs/src/test/groovy/org/spockframework/docs/interaction/GlobalMockDocSpec.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
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
39 changes: 39 additions & 0 deletions
39
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,50 @@ | ||
package org.spockframework.smoke.mock | ||
|
||
import org.spockframework.mock.CannotCreateMockException | ||
import org.spockframework.runtime.model.parallel.Resources | ||
import spock.lang.ResourceLock | ||
import spock.lang.Specification | ||
import spock.lang.Unroll | ||
|
||
class GroovyMocks extends Specification { | ||
def "implement GroovyObject"() { | ||
expect: | ||
GroovyMock(List) instanceof GroovyObject | ||
GroovyMock(ArrayList) instanceof GroovyObject | ||
} | ||
|
||
@ResourceLock(Resources.META_CLASS_REGISTRY) | ||
@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 org.spockframework.smoke.mock.GroovyMocks$LocalClassForMocking. 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, LocalClassForMocking) | ||
break | ||
case 'Stub': | ||
GroovyStub(global: true, LocalClassForMocking) | ||
break | ||
case 'Spy': | ||
GroovySpy(global: true, LocalClassForMocking) | ||
break | ||
default: | ||
throw new IllegalArgumentException(type) | ||
} | ||
} | ||
|
||
class LocalClassForMocking {} | ||
} |