Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clarify Documentation for global Mocks (#1755)
Added test case base documentation for global mocks. And clarify the usage of the global mock and lifecycle. This fixes #785
- Loading branch information
Showing
2 changed files
with
91 additions
and
17 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
57 changes: 57 additions & 0 deletions
57
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package org.spockframework.docs.interaction | ||
|
||
import spock.lang.Issue | ||
import spock.lang.Specification | ||
import spock.lang.Stepwise | ||
|
||
@Stepwise | ||
class GlobalMockDocSpec extends Specification { | ||
|
||
@Issue("https://github.com/spockframework/spock/issues/785") | ||
def "Global Spy usage"() { | ||
// tag::global-spy[] | ||
given: | ||
def publisher = new Publisher() | ||
def anySubscriber = GroovySpy(global: true, RealSubscriber) | ||
publisher.subscribers.add(new RealSubscriber()) | ||
publisher.subscribers.add(new RealSubscriber()) | ||
|
||
when: | ||
publisher.send("message") | ||
|
||
then: | ||
2 * anySubscriber.receive("message") | ||
// end::global-spy[] | ||
} | ||
|
||
def "Global GroovyMock mocks also constructor"() { | ||
// tag::global-mock-constructor[] | ||
given: | ||
GroovyMock(global: true, RealSubscriber) | ||
when: | ||
def sub = new RealSubscriber() | ||
then: "The GroovyMock(global: true) will also mock the constructor" | ||
sub == null | ||
// end::global-mock-constructor[] | ||
} | ||
|
||
def "Global GroovyMock but with real constructor"() { | ||
// tag::global-mock-constructor-real[] | ||
given: | ||
GroovyMock(global: true, RealSubscriber) { | ||
//Allow that the real constructor is called | ||
new RealSubscriber(*_) >> { callRealMethod() } | ||
} | ||
when: | ||
def sub = new RealSubscriber() | ||
then: | ||
sub instanceof RealSubscriber | ||
// end::global-mock-constructor-real[] | ||
} | ||
|
||
static class RealSubscriber implements Subscriber { | ||
@Override | ||
void receive(String message) { | ||
} | ||
} | ||
} |