我一直在尝试将方法返回Long,但我得到的只是null。有没有办法做到这一点?
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()
}
在这两个测试中,我都会收到以下错误:
Condition not satisfied:
currentTime == 1
| |
null false
答案 0 :(得分:2)
当您模拟并记录行为时,应定义如下。
以下是它的工作原理:
@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();
}