У меня есть экспорт в excel в моем приложении. Я должен написать тестовый пример для того же.Тест-код для экспорта в Excel Функциональность
Любые входы Образец кода ???
Heres мой код действия.
Когда пользователь нажимает кнопку «Загрузить на Excel» на странице, вызывается действие экспорта. Я должен проверить эту функциональность.
def export={
def playerId=session["playerId"]
//Calls calculateId, runs an sql query and returns a List
tuneInstanceList = tuneService.calculateId(playerId)
if(params?.format && params.format != "html"){
response.contentType = ConfigurationHolder.config.grails.mime.types[params.format]
response.setHeader("Content-disposition", "attachment; filename=tune.${params.extension}")
exportService.export(params.format, response.outputStream, tuneInstanceList,[:], [:])
}
[tuneInstanceList: tuneInstanceList]
session.flush()
session.clear()
}
Отредактировано:
Согласно ответу Robberts' написал интеграционный тест, как показано ниже.
class BinaryOutputControllerTests extends GroovyTestCase {
void testExportToExcel() {
def controller = new TuneController()
controller.session.playerID = "ABG65"
controller.params.format = "xls"
controller.params.extension = "xls"
def model = controller.export()
assert controller.response.status == 200
assert controller.response.contentAsByteArray.size() == 167336
assert controller.response.getContentType() \
== "application/vnd.ms-excel"
assert controller.response.getHeader("Content-disposition") \
== "attachment; filename=tune.${controller.params.extension}"
assert model.tuneInstanceList.size()
assert controller.session.valueNames.size() == 0
}
}
Однако, я получаю следующее сообщение об ошибке. Мысли?
TuneController.groovy: 169 относится к строке в экспорте, а именно:
exportService.export(params.format, response.outputStream, tuneInstanceList,[:], [:])
TuneControllerTests.groovy: 264 относится к линии в тестах, написанных именно.
def model = tuneController.export()
Ниже приведена ошибка, которую я получаю.
java.lang.reflect.UndeclaredThrowableException
at de.andreasschmitt.export.ExportService$$EnhancerByCGLIB$$cbf864b.export()
at de.andreasschmitt.export.ExportService$export.call(Unknown Source)
at pride.TuneController$_closure5.doCall(TuneController.groovy:169)
at pride.TuneController$_closure5.doCall(TuneController.groovy)
at pride.TuneControllerTests.testExportToExcel(TuneControllerTests.groovy:264)
Caused by: de.andreasschmitt.export.exporter.ExporterNotFoundException: No exporter found for type: xls
at de.andreasschmitt.export.exporter.DefaultExporterFactory.createExporter (DefaultExporterFactory.groovy:56)
at de.andreasschmitt.export.exporter.ExporterFactory$createExporter$0.callCurrent (Unknown Source)
at de.andreasschmitt.export.exporter.DefaultExporterFactory.createExporter (DefaultExporterFactory.groovy:23)
at de.andreasschmitt.export.exporter.ExporterFactory$createExporter.call(Unknown Source)
at de.andreasschmitt.export.ExportService.export(ExportService.groovy:19)
at de.andreasschmitt.export.ExportService$$FastClassByCGLIB$$c1bbbb10.invoke()
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
at de.andreasschmitt.export.ExportService$$EnhancerByCGLIB$$cbf864b.export()
at de.andreasschmitt.export.ExportService$export.call(Unknown Source)
at pride.TuneController$_closure5.doCall(TuneController.groovy:169)
at pride.TuneController$_closure5.doCall(TuneController.groovy)
at pride.TuneControllerTests.testExportToExcel(TuneControllerTests.groovy:264)
at _GrailsTest_groovy$_run_closure4.doCall(_GrailsTest_groovy:268)
at _GrailsTest_groovy$_run_closure4.call(_GrailsTest_groovy)
at _GrailsTest_groovy$_run_closure2.doCall(_GrailsTest_groovy:225)
at _GrailsTest_groovy$_run_closure1_closure21.doCall(_GrailsTest_groovy:184)
at _GrailsTest_groovy$_run_closure1.doCall(_GrailsTest_groovy:171)
at TestApp$_run_closure1.doCall(TestApp.groovy:101)
at gant.Gant$_dispatch_closure5.doCall(Gant.groovy:381)
at gant.Gant$_dispatch_closure7.doCall(Gant.groovy:415)
at gant.Gant$_dispatch_closure7.doCall(Gant.groovy)
at gant.Gant.withBuildListeners(Gant.groovy:427)
at gant.Gant.this$2$withBuildListeners(Gant.groovy)
at gant.Gant$this$2$withBuildListeners.callCurrent(Unknown Source)
at gant.Gant.dispatch(Gant.groovy:415)
at gant.Gant.this$2$dispatch(Gant.groovy)
at gant.Gant.invokeMethod(Gant.groovy)
at gant.Gant.executeTargets(Gant.groovy:590)
at gant.Gant.executeTargets(Gant.groovy:589)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'xlsExporter' is defined
at de.andreasschmitt.export.exporter.DefaultExporterFactory.createExporter (DefaultExporterFactory.groovy:34)
Испытательные случаи, как правило, получены из случаев использования, а не только из воздуха ... –
Thats right Belisarius. обновили мой код действия также. Пожалуйста, посмотрите. – MAlex
[-1] Вопросник расширил свой вопрос далеко за рамки оригинала. Ответчик ответил, но этот вопрос не вернулся. – robbbert