简体   繁体   English

我应该单元测试生成的Java代码吗?

[英]Should I unit test generated Java code?

Simple question. 简单的问题。 If I use spring-data to generate CRUD methods for my DAO layer, should I still write unit tests against the generated methods? 如果我使用spring-data为我的DAO层生成CRUD方法,我还应该针对生成的方法编写单元测试吗? Or would that be the equivalent of unit testing library code? 或者那相当于单元测试库代码?

Thanks in advance. 提前致谢。

EDIT: To clarify, I'm asking whether or not the unit test needs to be written in addition to a suite of integration tests that get run before a release. 编辑:为了澄清,我问的是,除了在发布之前运行的一套集成测试之外,是否还需要编写单元测试。 For example, a unit test for the findAll() method of the DAO layer would be similar to the following: 例如,DAO层的findAll()方法的单元测试类似于以下内容:

class DepartmentDAOTest extends spock.lang.Specification {
   /* ... */
   def "returns all departments"() {
       setup:
       def result = new List<Department>()
       when:
       result = dao.findAll()
       then:
       result.size() == EXPECTED_SIZE
   }
}

Whereas an integration test would be run probably by a test team or developer by hand, possibly before tagging a new release. 然而,在标记新版本之前,可能由测试团队或开发人员手动运行集成测试 This could either be automated using JWebUnit or Geb , and tests every component (including the platform) to ensure they work as expected when "integrated." 这可以使用JWebUnitGeb自动化,并测试每个组件(包括平台),以确保它们在“集成”时按预期工作。

If I were to write the DAO implementation by hand using JdbcTemplate there would be no question that I should unit test every method. 如果我使用JdbcTemplate手动编写DAO实现,那么毫无疑问我应该对每个方法进行单元测试。 When I unit test the service layer (which makes calls to the DAO layer) I can mock out the DAO layer so I don't test it twice. 当我对服务层(调用DAO层)进行单元测试时,我可以模拟DAO层,所以我不测试它两次。

If I make a call into a third-party library like pdfbox for generating a PDF, there's an expectation for each method to work (because it is tested as a part of the pdfbox project). 如果我打电话到pdfbox这样的第三方库来生成PDF,那么每种方法都有一个期望(因为它是作为pdfbox项目的一部分进行测试的)。 I don't test that their drawSquare method really draws a square, but during integration testing I'll see that my export PDF functionality correctly exports a PDF the way we want it to. 我不测试他们的drawSquare方法确实绘制了正方形,但在集成测试期间,我会看到我的导出PDF功能正确地按照我们想要的方式导出PDF。

So the question should really be re-worded as, "Under which testing phase should I test my usage of spring-data?" 所以这个问题应该重新措辞,“我应该在哪个测试阶段测试我对弹簧数据的使用?”

不可以。作为一般规则,不要测试平台。

First, there is no code generated at all. 首先,根本没有生成代码。 We built a query meta model from the query methods you declare and dynamically execute these queries. 我们根据您声明的查询方法构建了一个查询元模型,并动态执行这些查询。 The short answer here is: you definitely should test these methods declared. 这里简短的回答是:你肯定应该测试这些声明的方法。 The reason is as obvious as it is simple: the query method declarations - no matter if they use derived queries or manually declared ones - interact with the mapping metadata you defined for your entities. 原因很明显,因为它很简单:查询方法声明 - 无论是使用派生查询还是手动声明的 - 都与您为实体定义的映射元数据进行交互。 Thus, it's definitely reasonable to check the query method execution to make sure you see the expected results. 因此,检查查询方法执行以确保您看到预期结果是绝对合理的。 This then of course an more of an integration test and a semantical check for the queries executed, rather than a classical unit test. 这当然是更多的集成测试和对执行的查询的语义检查,而不是经典的单元测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM