Skip to content

Commit

Permalink
Fix minor things
Browse files Browse the repository at this point in the history
  • Loading branch information
leonard84 committed Dec 28, 2024
1 parent 7d2f94f commit c148647
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class JavaSpies extends Specification {
}
def "construct spied-on object using provided constructor args"() {
given:
Constructable spy = Spy(constructorArgs: ctorArgs)
expect:
Expand All @@ -51,6 +52,7 @@ class JavaSpies extends Specification {
}
def "call real methods by default"() {
given:
Person person = Spy(constructorArgs: ["fred", 42])
expect:
Expand All @@ -59,6 +61,7 @@ class JavaSpies extends Specification {
}
def "call real equals method by default"() {
given:
Person fred1 = Spy(constructorArgs: ["fred", 42])
Person fred2 = Spy(constructorArgs: ["fred", 21])
Person barney = Spy(constructorArgs: ["barney", 33])
Expand All @@ -69,20 +72,23 @@ class JavaSpies extends Specification {
}
def "call real hashCode method by default"() {
given:
Person person = Spy(constructorArgs: ["fred", 42])
expect:
person.hashCode() == "fred".hashCode()
}
def "call real toString method by default"() {
given:
Person person = Spy(constructorArgs: ["fred", 42])
expect:
person.toString() == "Hi, I'm fred"
}
def "can verify interactions with real methods"() {
given:
Person person = Spy(constructorArgs: ["fred", 42])
when:
Expand All @@ -96,6 +102,7 @@ class JavaSpies extends Specification {
}
def "can be used as partial mocks"() {
given:
def person = Spy(Person, constructorArgs: ["fred", 42]) {
getWorkHours() >>> [3, 2, 1]
}
Expand All @@ -107,6 +114,7 @@ class JavaSpies extends Specification {
}
def "can spy on concrete instances"() {
given:
def person = Spy(new Person())
when:
Expand All @@ -119,6 +127,7 @@ class JavaSpies extends Specification {
@Issue("https://github.com/spockframework/spock/issues/1035")
def "using >>_ does not call original method and produces stubbed value"() {
given:
def person = Spy(new Person())
when:
Expand All @@ -138,6 +147,7 @@ class JavaSpies extends Specification {
@Issue("https://github.com/spockframework/spock/issues/771")
def "spying on concrete instances can use partial mocking"() {
given:
def person = Spy(new Person())
when:
Expand Down Expand Up @@ -186,6 +196,7 @@ class JavaSpies extends Specification {
}
def "can spy final methods as property with mockito"() {
given:
FinalMethodPerson person = Spy(mockMaker: MockMakers.mockito)
person.phoneNumber >> 6789
Expand All @@ -194,6 +205,7 @@ class JavaSpies extends Specification {
}
def "can spy final methods with mockito"() {
given:
FinalMethodPerson person = Spy(mockMaker: MockMakers.mockito)
person.getPhoneNumber() >> 6789
Expand All @@ -202,6 +214,7 @@ class JavaSpies extends Specification {
}
def "can spy final methods with mockito with closure"() {
given:
FinalMethodPerson person = Spy(mockMaker: MockMakers.mockito) {
phoneNumber >> 6789
}
Expand All @@ -212,6 +225,7 @@ class JavaSpies extends Specification {
@Issue("https://github.com/spockframework/spock/issues/2039")
def "cannot spy on final methods with byteBuddy"() {
given:
FinalMethodPerson person = Spy(mockMaker: MockMakers.byteBuddy)
when:
Expand All @@ -227,6 +241,7 @@ class JavaSpies extends Specification {
@Issue("https://github.com/spockframework/spock/issues/2039")
def "cannot spy on final methods as property with byteBuddy"() {
given:
FinalMethodPerson person = Spy(mockMaker: MockMakers.byteBuddy)

when:
Expand All @@ -241,6 +256,7 @@ class JavaSpies extends Specification {
}

def "cannot spy on final methods without specifying mockMaker"() {
given:
FinalMethodPerson person = Spy()

when:
Expand Down Expand Up @@ -299,15 +315,17 @@ class JavaSpies extends Specification {

def "no static type specified"() {
when:
Stub()
Spy()

then:
InvalidSpecException ex = thrown()
ex.message == "Mock object type cannot be inferred automatically. Please specify a type explicitly (e.g. 'Mock(Person)')."
ex.message == "Spy object type cannot be inferred automatically. Please specify a type explicitly (e.g. 'Spy(Person)')."
}

def "specified instance is null"() {
when:
Spy((Object) null)

then:
SpockException ex = thrown()
ex.message == "Spy instance may not be null"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ class JavaStubs extends Specification {
}

def "can be stubbed (using property syntax)"() {
given:
person.name >> "fred"
expect:
person.name == "fred"
}
def "can be stubbed (using method syntax)"() {
given:
person.getName() >> "fred"
expect:
Expand Down Expand Up @@ -77,6 +79,7 @@ class JavaStubs extends Specification {
}
def "like to be stubbed at creation time"() {
given:
person = Stub(IPerson) {
getName() >> "fred"
}
Expand All @@ -87,6 +90,7 @@ class JavaStubs extends Specification {
@FailsWith(InvalidSpecException)
def "cannot be mocked"() {
given:
1 * person.name >> "fred"
expect:
Expand Down Expand Up @@ -134,6 +138,7 @@ class JavaStubs extends Specification {
}
def "can stub final methods as property with mockito"() {
given:
FinalMethodPerson person = Stub(mockMaker: MockMakers.mockito)
person.phoneNumber >> 6789
Expand Down Expand Up @@ -268,7 +273,7 @@ class JavaStubs extends Specification {

then:
InvalidSpecException ex = thrown()
ex.message == "Mock object type cannot be inferred automatically. Please specify a type explicitly (e.g. 'Mock(Person)')."
ex.message == "Stub object type cannot be inferred automatically. Please specify a type explicitly (e.g. 'Stub(Person)')."
}

interface IPerson {
Expand Down

0 comments on commit c148647

Please sign in to comment.