可以嘲笑模拟最后的课程吗?如果是这样,怎么样?搜索结果提出了这个要点,这似乎意味着如此,但我找不到任何这样做的例子.我还发现论坛帖子说不支持模拟最终类.
本规范:
@Grab('org.spockframework:spock-core:1.0-groovy-2.4')
@Grab('cglib:cglib-nodep:3.1')
import spock.lang.*
class Test extends Specification {
def 'lol'() {
given:
def s = Mock(String) {
size() >> 10
}
expect:
s.size() == 10
}
}
以以下例外结束:
JUnit 4 Runner, Tests: 1, Failures: 1, Time: 29 Test Failure:
lol(Test) org.spockframework.mock.CannotCreateMockException:
Cannot create mock for class java.lang.String because Java mocks cannot mock final classes.
If the code under test is written in Groovy, use Groovy mock.
解决方案是使用GroovyMock
:
@Grab('org.spockframework:spock-core:1.0-groovy-2.4')
@Grab('cglib:cglib-nodep:3.1')
import spock.lang.*
class Test extends Specification {
def 'lol'() {
given:
def s = GroovyMock(String) {
size() >> 10
}
expect:
s.size() == 10
}
}
哪个效果很好.