Groovy spock 测试未正确存根 -- 收到 null
Groovy spock test isn't stubbing correctly -- receiving null
出于某种原因(我对 Spock 不是很好,并且是模拟和存根的新手)我无法获得特定的方法来存根我想要的东西。
我已经缩短了一点,但基本上我有一个搜索返回的书籍列表。每本书都有一个workId
。这本书可能有较旧或较新的版本,但它们都具有相同的 workId
-- 如果用户选择,他们可以获取所有其他版本。
前言
Class:版本
class Edition {
//some strings, ints, and booleans -- information about the edition
}
Class: EditionResults 获取单个作品的额外版本
class EditionResults {
List<Edition> editionsList
String parentWorkId
}
最后一个 class:BulkEditions 用于打包多部作品(搜索结果页面上有 10 本书)
class BulkEditions {
List<EditionResults> bulkEditions
}
这个控制器是我遇到问题的地方
@RequestMapping('/api')
class EditionsController {
@Autowired
private EditionsService editionsService
private Logger logger = LoggerFactory.getLogger(EditionsController)
@RequestMapping(value = '/bulkeditions', produces = 'application/json')
ResponseEntity<BulkEditions> getBulkEditions(
@RequestParam(value = 'workIds') Set<String> workIds
) throws Exception
{
BulkEditions newBulkEditions = new BulkEditions(bulkEditions: [])
workIds.each { workId ->
EditionResults editions = editionsService.searchEditions(workId)
logger.info "EditionResults {}", editions
if (editions?.editionsList) {
newBulkEditions.bulkEditions << editions
}
}
new ResponseEntity<BulkEditions>(newBulkEditions, HttpStatus.OK)
}
}
特别是,我在测试 editions?.editionsList
的真实分支时遇到了问题——出于某种原因,我一直收到 null
for editions
这是我的测试结果(总是通过,但 clover 覆盖失败)
class EditionsControllerSpec extends Specification {
private final EditionsService editionsService = Mock(EditionsService)
private static final Set<String> WORK_ID_SET = '12345' as Set<String>
private static final String SINGULAR_WORK_ID = '12345'
@Subject
private final EditionsController controller = new EditionsController(editionsService: editionsService)
void 'test get bulk editions'() {
given:
EditionResults editionResults = Mock(EditionResults)
Edition edition = GroovyMock(Edition) //it's a java class
edition.isAvailable >> true
//edition.bunchOfOtherProps >> randomValues
editionResults.editionsList >> [edition]
editionResults.parentWorkId >> SINGULAR_WORK_ID
editionsService.searchEditions(SINGULAR_WORK_ID) >> editionResults
when:
ResponseEntity<BulkLocalEditions> response = controller.getBulkEditions(WORK_ID_SET)
then:
response
}
我的问题是这部分:
editionsService.searchEditions(SINGULAR_WORK_ID) >> editionResults
我不明白为什么我一直(从我的记录器)得到这个
[Test worker] INFO [EditionsController:100] [doCall] EditionResults ***null***
我的理解是 editionsService.searchEditions(SINGULAR_WORK_ID) >> editionResults
相当于说“当我们使用这个 searchEditions
方法时,它的结果是 editionResults
”——我们已经填充了... . 怎么会是null呢?
我已尝试尽可能详细和清晰地回答这个问题,为简洁起见进行了简化,但如果需要任何其他信息,请告诉我 - 当然提前致谢。
还值得注意的是,它实际上适用于我的本地设备,并且一切正常返回——正是这种三叶草覆盖让我受益
感谢@tim_yates
editionsService.searchEditions(SINGULAR_WORK_ID) >> editionResults
应该在我的 then
区块中
出于某种原因(我对 Spock 不是很好,并且是模拟和存根的新手)我无法获得特定的方法来存根我想要的东西。
我已经缩短了一点,但基本上我有一个搜索返回的书籍列表。每本书都有一个workId
。这本书可能有较旧或较新的版本,但它们都具有相同的 workId
-- 如果用户选择,他们可以获取所有其他版本。
前言
Class:版本
class Edition {
//some strings, ints, and booleans -- information about the edition
}
Class: EditionResults 获取单个作品的额外版本
class EditionResults {
List<Edition> editionsList
String parentWorkId
}
最后一个 class:BulkEditions 用于打包多部作品(搜索结果页面上有 10 本书)
class BulkEditions {
List<EditionResults> bulkEditions
}
这个控制器是我遇到问题的地方
@RequestMapping('/api')
class EditionsController {
@Autowired
private EditionsService editionsService
private Logger logger = LoggerFactory.getLogger(EditionsController)
@RequestMapping(value = '/bulkeditions', produces = 'application/json')
ResponseEntity<BulkEditions> getBulkEditions(
@RequestParam(value = 'workIds') Set<String> workIds
) throws Exception
{
BulkEditions newBulkEditions = new BulkEditions(bulkEditions: [])
workIds.each { workId ->
EditionResults editions = editionsService.searchEditions(workId)
logger.info "EditionResults {}", editions
if (editions?.editionsList) {
newBulkEditions.bulkEditions << editions
}
}
new ResponseEntity<BulkEditions>(newBulkEditions, HttpStatus.OK)
}
}
特别是,我在测试 editions?.editionsList
的真实分支时遇到了问题——出于某种原因,我一直收到 null
for editions
这是我的测试结果(总是通过,但 clover 覆盖失败)
class EditionsControllerSpec extends Specification {
private final EditionsService editionsService = Mock(EditionsService)
private static final Set<String> WORK_ID_SET = '12345' as Set<String>
private static final String SINGULAR_WORK_ID = '12345'
@Subject
private final EditionsController controller = new EditionsController(editionsService: editionsService)
void 'test get bulk editions'() {
given:
EditionResults editionResults = Mock(EditionResults)
Edition edition = GroovyMock(Edition) //it's a java class
edition.isAvailable >> true
//edition.bunchOfOtherProps >> randomValues
editionResults.editionsList >> [edition]
editionResults.parentWorkId >> SINGULAR_WORK_ID
editionsService.searchEditions(SINGULAR_WORK_ID) >> editionResults
when:
ResponseEntity<BulkLocalEditions> response = controller.getBulkEditions(WORK_ID_SET)
then:
response
}
我的问题是这部分:
editionsService.searchEditions(SINGULAR_WORK_ID) >> editionResults
我不明白为什么我一直(从我的记录器)得到这个
[Test worker] INFO [EditionsController:100] [doCall] EditionResults ***null***
我的理解是 editionsService.searchEditions(SINGULAR_WORK_ID) >> editionResults
相当于说“当我们使用这个 searchEditions
方法时,它的结果是 editionResults
”——我们已经填充了... . 怎么会是null呢?
我已尝试尽可能详细和清晰地回答这个问题,为简洁起见进行了简化,但如果需要任何其他信息,请告诉我 - 当然提前致谢。
还值得注意的是,它实际上适用于我的本地设备,并且一切正常返回——正是这种三叶草覆盖让我受益
感谢@tim_yates
editionsService.searchEditions(SINGULAR_WORK_ID) >> editionResults
应该在我的 then
区块中