gpt4 book ai didi

java - 模拟在 Spock 框架中不起作用

转载 作者:行者123 更新时间:2023-11-30 02:50:35 24 4
gpt4 key购买 nike

我正在尝试编写 Spock Framework 而不是 Junit,

测试类:

class StudentServiceSpec extends Specification{

@Shared def studentDao
@Shared def studentService

def setupSpec(){
studentDao = Mock(StudentDao)
studentService = new StudentService(studentDao)
}

def "Get Student Details Based on StudentId"(){

setup:
1*studentDao.getStudent(67) >> new Student()

when:
Response response = studentService.getStudent("67")
println "** Response "+response
println "** Response "+response.getEntity()

then:
response != null
}
}

当我使用 maven clean install 命令运行上述代码时,出现以下错误。

错误:

    1*studentDao.getStudent(67) >>> new Student()   (0 invocations)

如果我使用0*studentDao.getStudent(67) >>> new Student()我得到 response.getEntity() is null

最佳答案

我发现了我的错误...

我替换了以下代码

@Shared def studentDao
@Shared def studentService

def setupSpec(){
studentDao = Mock(StudentDao)
studentService = new StudentService(studentDao)
}

用这两行

 StudentDao studentDao = Mock()
StudentService studentService = new StudentService(studentDao)

如果我们使用@Shared,它会模拟,但不会模拟方法调用

关于java - 模拟在 Spock 框架中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38782769/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com