Reputation: 800
I have been trying to stub method returning Long but all I get is null. Is there a way to do this?
interface Clock {
Long currentTimeMillis();
}
def "stub method returning long"() {
Clock clock = Mock(Clock)
clock.currentTimeMillis() >> 1
when:
Long currentTime = clock.currentTimeMillis()
then:
currentTime == 1
1 * clock.currentTimeMillis()
}
def "mock method returning longs"() {
Clock clock = Mock(Clock)
clock.currentTimeMillis() >>> [1, 2, 3]
when:
Long currentTime = clock.currentTimeMillis()
then:
currentTime == 1
1 * clock.currentTimeMillis()
}
In both tests I'm getting following error:
Condition not satisfied:
currentTime == 1
| |
null false
Upvotes: 0
Views: 527
Reputation: 84824
When You both mock and record the behavior, it should be defined as below.
Here's how it works:
@Grab('org.spockframework:spock-core:0.7-groovy-2.0')
@Grab('cglib:cglib-nodep:3.1')
import spock.lang.*
class Test extends Specification {
def "stub method returning long"() {
given:
Clock clock = Mock(Clock)
when:
Long currentTime = clock.currentTimeMillis()
then:
currentTime == 1
1 * clock.currentTimeMillis() >> 1
}
def "mock method returning longs"() {
given:
Clock clock = Mock(Clock)
when:
Long currentTime = clock.currentTimeMillis()
then:
currentTime == 1
1 * clock.currentTimeMillis() >>> [1, 2, 3]
}
}
interface Clock {
Long currentTimeMillis();
}
Upvotes: 2