我在应用程序中导出了excel功能。我要写一个测试用例。
任何输入??示例代码??
这是我的行动代码。
当用户点击页面上的“下载到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)
答案 0 :(得分:2)
在技术层面,必须决定是否使用 integration 或 unit 测试。
在Grails单元测试中,依赖注入(例如服务)不可用(但是,测试类可以手动为控制器提供依赖性)。任何其他类型的仪器,包括数据库访问,也不可用 在我个人看来,最好用控制器编写更精细的(单元)测试;但是,如果您不想模拟您的服务,集成测试也会这样做。
以下示例代码是集成测试(但单元测试不是 不同):
class BinaryOutputControllerTests extends GroovyTestCase {
void testExportToExcel() {
def controller = new BinaryOutputController()
controller.session.playerID = "somePlayerID"
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
}
}
示例代码显示您可以访问GrailsHttpSession和控制器的params
对象来初始化值。
接下来,您将调用控制器操作方法并检索其返回值。
在测试断言中,您可以访问控制器的MockHttpServletResponse来查询响应值。同样,GrailsHttpSession对象以及控制器的操作方法返回的模型也可用。
显然,您需要识别用例;然而,这些是一些技术基础。您还应该使用shouldFail { ... }
闭包来编写失败的测试。看看我提供的链接,因为它们会为您提供其他选项的提示。
编辑:根据您的进一步询问:
首先,让您的ExporterNotFoundException
extend RuntimeException
(而不是已检查 Exception
)以避免UndeclaredThrowableException
。或者(不是首选),将throws ExporterNotFoundException
子句添加到DefaultExporterFactory.createExporter(..)
方法定义中。 - Groovy都基于RuntimeException
,但您仍然需要使用Exception
子句明确声明已检查 throws
。
其次,检查您的ExportService
是否能够处理“xls”format
(查找ExporterNotFoundException
的原因)。显然,事实并非如此。
答案 1 :(得分:0)
belisarius暗示的是我们无法真正提供您正在寻找的东西。对于测试用例(或者您的意思是测试数据?),您应该真正关注系统的用例。在编码之前应该知道用例(即,我的用户将如何使用此应用程序)。您或您团队中的某个人应该知道这应该如何工作,并且应该能够帮助您找到您要找的东西。如果您或团队成员不知道这应该如何工作,那就找一个做的人;我们(一般IT公众)肯定无法帮助。
一旦你知道它应该如何工作,你应该能够找到/制作样本数据来使用。