Testing 导出到Excel功能的测试用例

Testing 导出到Excel功能的测试用例,testing,grails,case,export-to-excel,Testing,Grails,Case,Export To Excel,我的应用程序中有导出到excel的功能。我将为它们编写一个测试用例 任何输入??样本代码 这是我的行动代码 当用户单击页面上的下载到excel时,将调用下面的导出操作。我将测试此功能 def export={ def playerId=session["playerId"] //Calls calculateId, runs an sql query and returns a List tuneInstanceList = tuneService.calculate

我的应用程序中有导出到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 
} 
}

但是,我得到了以下错误。想法

groovy:169指导出操作中的行,即

exportService.export(params.format, response.outputStream, tuneInstanceList,[:], [:])
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)

贝里萨里乌斯暗示的是,我们不能真正提供你想要的东西。对于测试用例,还是指测试数据?您应该真正了解系统的用例。在编写ie之前,应该知道用例,我的用户将如何使用这个应用程序。你或你团队中的某个人应该知道这是如何工作的,并且应该能够帮助你找到你想要的东西。如果你或团队成员不知道这是如何工作的,找一个知道的人;我们普通的IT公众当然无能为力


一旦你知道了它应该如何工作,你就应该能够找到/制作样本数据来使用。

在技术层面,必须决定是使用测试还是使用测试

例如,在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
    }
}
示例代码显示您可以访问和控制器的params对象来初始化值

接下来,调用controllers操作方法并检索其返回值

在测试断言中,您可以访问控制器的以查询响应值。同样,对象和控制器的动作方法返回的模型都可用

显然,您需要识别用例;然而,这些是一些技术基础。您还应该使用shouldFail{…}闭包编写失败的测试。请看我提供的链接,它们为您提供了关于其他选项的提示

编辑:根据您的进一步查询:

首先,让您的ExporterNotFoundException扩展RuntimeException而不是checked异常以避免这种情况。或者,将抛出ExporterNotFoundException子句添加到DefaultExporterFactory.createExporter。。方法定义Groovy都基于RuntimeExceptions,但是您仍然需要使用throws子句明确地声明已检查的异常


其次,检查您的ExportService是否能够处理xls格式查找ExporterNotFoundException的原因。显然,事实并非如此。

测试用例通常是从用例中派生出来的,而不仅仅是凭空而来的……贝里萨利斯就是这样。我也更新了我的行动代码。请看。[-1]提问者将他们的问题扩展到了原来的范围之外。回答者回答,但发问者还没有回来。@robbert:我的坏…力c这已经有一段时间了…让我试试这个…Sumhw当有人在我的qn上发言/评论时,我不会弹出一个弹出窗口…这很糟糕这正是我要找的robbert…我完全理解你的观点,确定用例并开始行动。然而,这里有一个查询。在我的控制器中,在导出操作的最后一行中,调用了exportService。这里我的集成测试给了我一些问题。我已经用错误代码更新了我原来的问题。你能检查一下吗?谢谢,马特。我听到你在说什么了。我实际上是在寻找一个基本的模型。例如,在我的例子中,用例是检查excel是否有一些数据。数据细节并不重要。但事实上,excel正在被填充就可以了。