java 如何测试 jasper 报告?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3406638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 01:42:05  来源:igfitidea点击:

How can I test a jasper report?

javaunit-testingtestingjasper-reports

提问by

I want to test all the jasper reports of my application. I want to be able to detect:

我想测试我的应用程序的所有 jasper 报告。我希望能够检测到:

  • Compilation problems (Would checking that JasperCompileManager.compileReport(some inputStream)doesn′t throw JRExceptionis a good option for this?)
  • Filling problems (Would checking that JasperFillManager.fillReport(someReport, someParameters, someDataSource)doesn′t throw JRExceptionis a good option for this?)
  • Rendering problems: detect "null" strings appearing, text looking truncated in the exported PDF file
  • Any other idea?
  • 编译问题(检查JasperCompileManager.compileReport(some inputStream)不抛出JRException是否是一个不错的选择?)
  • 填充问题(检查JasperFillManager.fillReport(someReport, someParameters, someDataSource)不抛出JRException是否是一个不错的选择?)
  • 渲染问题:检测出现的“空”字符串,导出的 PDF 文件中的文本看起来被截断
  • 还有其他想法吗?

Ideally I would like to keep the testing process the most generic possible. The thing is that each jasper has a different data source so for detecting this errors i need to generate at list some data (right?) and this data I need to generate is different for each jasper of my application. So i don′t know how much testing code I would be able to reuse. What approach would you take?

理想情况下,我希望保持测试过程尽可能通用。问题是每个 jasper 都有不同的数据源,所以为了检测这个错误,我需要在列表中生成一些数据(对吧?),我需要生成的数据对于我的应用程序的每个 jasper 都是不同的。所以我不知道我可以重用多少测试代码。你会采取什么方法?

Thanks!

谢谢!

回答by yankee

For compilation problems and for filling problems: Yes, you can do as described and just try to compile and fill the report. If that does not throw an Exception, then you are fine.

对于编译问题和填充问题:是的,您可以按照描述进行操作,只需尝试编译和填充报告即可。如果那没有抛出异常,那么你没问题。

For the third step of checking rendering problems, I export the report to XML:

对于检查渲染问题的第三步,我将报告导出为 XML:

String xml = JasperExportManager.exportReportToXml(
    JasperFillManager.fillReport(report, params, dataSource)
);

Theoretically the XML is an accurate representation of the PDF file, but in a format which is easy to read in tests. E.g. if a field is too long and the content is truncated, then it is also truncated in the XML. All values are already in the "rendered" format, so if you e.g. have a number that you format with a (DecimalFormat)-pattern, then the number will be formatted in the XML. Also the uuid-identifiers for fields are still present in the XML, so you can also easily find the fields. To check a field, you can e.g. do something along the lines of:

理论上,XML 是 PDF 文件的准确表示,但格式在测试中易于阅读。例如,如果字段太长并且内容被截断,那么它也会在 XML 中被截断。所有值都已采用“渲染”格式,因此,例如,如果您有一个使用 (DecimalFormat) 模式格式化的数字,则该数字将在 XML 中进行格式化。此外,字段的 uuid 标识符仍然存在于 XML 中,因此您也可以轻松找到这些字段。要检查字段,您可以例如执行以下操作:

hasXPath("//*[@uuid = '" + field.getUuid() + "']/../*[local-name() = 'textContent']", matcher)

回答by garfiny

I created a couple of unit test for testing aspects.

我创建了几个单元测试来测试方面。

  1. Compilation problems: I created a class to load and compile all of my templates. My enviornment is web-based. So I tested the path through this as well.

  2. For filling: I mocked data and JRDataSource to fill a template, that works find for me.

  3. For Rendering: I haven't found a good approach to do it. Anyone got ideas?

  1. 编译问题:我创建了一个类来加载和编译我的所有模板。我的环境是基于网络的。所以我也测试了这条路。

  2. 填充:我模拟了数据和 JRDataSource 来填充模板,这对我有用。

  3. 对于渲染:我还没有找到一个好的方法来做到这一点。有人有想法吗?

And also, I usually use JavaBean data source so that I can test the reports using a traditional java unit test method.

而且,我通常使用 JavaBean 数据源,以便我可以使用传统的 java 单元测试方法测试报告。

回答by Michele Milesi

For automatic compilation you may try to compile reports with Ant script (see 1or http://jasperreports.sourceforge.net/api/net/sf/jasperreports/ant/package-summary.html)

对于自动编译,您可以尝试使用 Ant 脚本编译报告(参见1http://jasperreports.sourceforge.net/api/net/sf/jasperreports/ant/package-summary.html

Ant task is suitable to be used within automatic building or continuous integration system like maven or jenkins.

Ant 任务适用于自动构建或持续集成系统,如 maven 或 jenkins。

回答by frandevel

One approach for the rendering could be to export the report to PDF and then use something like PDFBox (Apache) to test on the contents. Also you could do the same with exporting to Excel file and then use Apache POI or JExcelAPI to test on the cell contents.

呈现的一种方法可能是将报告导出为 PDF,然后使用类似 PDFBox (Apache) 之类的工具来测试内容。您也可以对导出到 Excel 文件执行相同操作,然后使用 Apache POI 或 JExcelAPI 对单元格内容进行测试。

Regards

问候

Fran

弗兰

回答by Jasper Vandaele

Regarding the PDFbox approach to test the content of PDF documents: I just discovered the jPdfUnit opensource project which helps greatly:

关于测试 PDF 文档内容的 PDFbox 方法:我刚刚发现了 jPdfUnit 开源项目,它有很大帮助:

Check out the docs here: http://jpdfunit.sourceforge.net/docs/GettingStarted.html

在此处查看文档:http: //jpdfunit.sourceforge.net/docs/GettingStarted.html

It's based on junit 3 and hasen't been maintained in years, but it just works. One tweak I had to apply to get it working was to set the pdfbox version to 1.8.13. Just put this in your if you are using maven:

它基于junit 3,并且已经多年没有维护了,但它确实有效。我必须应用以使其正常工作的一项调整是将 pdfbox 版本设置为 1.8.13。如果您使用的是 maven,只需将其放入您的:

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.apache.pdfbox</groupId>
                <artifactId>pdfbox</artifactId>
                <!-- jpdfunit is not compatible with pdfbox 2 (java5 vs java6) -->
                <version>1.8.13</version>
            </dependency>
        </dependencies>
</dependencyManagement>

回答by Manjum

For compilation problems , one approach would be to use iReport tool and all report templates are compiles successfully; assuming that you are using templates and then populating data into it.

对于编译问题,一种方法是使用iReport工具,所有的报表模板都编译成功;假设您正在使用模板,然后将数据填充到其中。

I found the following link useful ,

我发现以下链接很有用,

http://flexingcode.blogspot.com/2009/04/junit-for-jasper-reports.html

http://flexingcode.blogspot.com/2009/04/junit-for-jasper-reports.html

Hope that helps,

希望有所帮助,

Manju

馒头