この(正常に動作する)Grailsサービスの単体テストを作成しています:
class CommonCodeService {
def gridUtilService
def getList(def params){
def ret = null
try {
def res = gridUtilService.getDomainList(CommonCode, params)
def rows = []
def counter = 0
res?.rows?.each{ // this is line 15
rows << [
id: counter ++,
cell:[
it.key,
it.value
]
]
}
ret = [rows: rows, totalRecords: res.totalRecords, page: params.page, totalPage: res.totalPage]
} catch (e) {
e.printStackTrace()
throw e
}
return ret
}
}
これはコラボレーターからのメソッドですGridUtilService
:
import grails.converters.JSON
class GridUtilService {
def getDomainList(Class domain, def params){
/* some code */
return [rows: rows, totalRecords: totalRecords, page: params.page, totalPage: totalPage]
}
}
そして、これは私の(動作していない)単体テストです:
import grails.test.mixin.TestFor
import grails.test.mixin.Mock
import com.emerio.baseapp.utils.GridUtilService
@TestFor(CommonCodeService)
@Mock([CommonCode,GridUtilService])
class CommonCodeServiceTests {
void testGetList() {
def rowList = [new CommonCode(key: 'value', value: 'value')]
def serviceStub = mockFor(GridUtilService)
serviceStub.demand.getDomainList {Map p -> [rows: rowList, totalRecords: rowList.size(), page:1, totalPage: 1]}
service.gridUtilService = serviceStub.createMock()
service.getList() // this is line 16
}
}
テストを実行すると、例外が表示されます:
No such property: rows for class: com.emerio.baseapp.CommonCodeServiceTests
groovy.lang.MissingPropertyException: No such property: rows for class: com.emerio.baseapp.CommonCodeServiceTests
at com.emerio.baseapp.CommonCodeService.getList(CommonCodeService.groovy:15)
at com.emerio.baseapp.CommonCodeServiceTests.testGetList(CommonCodeServiceTests.groovy:16)
の代わりにモックされた returnGridUtilService
インスタンスCommonCodeServiceTests
のようですMap
。単体テストの何が問題になっていますか?