在hibernate领域类中会出现什么问题 - 这样我们需要(单元)测试它们吗? [英] What can go wrong in hibernate domain classes – so that we need to (unit) test them?

查看:89
本文介绍了在hibernate领域类中会出现什么问题 - 这样我们需要(单元)测试它们吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始为我的新J2EE项目编写hibernate域类,并且在我最初的分析过程中,我感觉到在域类中出现以下情况可能会出现问题,这些域名类可能会让我为自己写入自动化测试以提前捕获它们:


  1. 属性/列映射 - 我可能会错误地将错误的列配置为域类中的给定属性
  2. 我可能会更改数据库列名并忘记更改相应的映射(s)域类文件

  3. 我可能会忘记在构造函数中设置对象属性。这可能发生在创建类的时候,或者在测试某些属性时通过注释掉某些属性而忘记取消注释。 (我在现实生活中犯了这个错误)。

  4. 我没有为相应的数据库列类型定义具有正确数据类型的属性。 >边界条件测试以确保我们为属性定义了适当大小的数据类型。这还将捕获将来对数据类型的任何无意的更改,例如从long到int。

是否测试域类这些更改称为单元测试或不,我不太确定,因为它在测试时涉及数据库。我需要的是一种测试我的领域类的方法,以确保它们不因上述任何上述变化而无法正常工作。



重要假设:数据库实体(表,约束,索引)已经准确创建,并且没有错误,因为数据类型,大小等已经被精确定义。
换句话说,作为域类的(单元)测试的一部分,我们不会测试潜在错误的模式。下面的例子:
*数据类型及其大小是正确的
*如果需要约束(PK,FK,UK,索引等)存在并且它们是正确的


自动测试域类



对于问题1,2,4 -
使用:在hibernate中验证配置文件。
在相关表格中保存记录(只是必填字段就足够了)的简单测试将确保这些问题不存在于映射中。断言:不应该引发任何异常。



对于问题3 - 如果有任何字段被忽略,则简单测试用于保存包含所有字段的记录。断言:不应该引发任何异常。



对于问题5 - 需要测试用例插入带有最大边界条件的所有字段的记录。断言:不应该引发任何异常。



摘要:用所有填充了最大边界值的字段保存记录的测试用例将足以测试休眠域类。断言:不应该引发任何异常。



问题


  1. 上述说法有意义吗?如果没有,为什么?

  2. 如果有意义,实际实现的正确技术是什么?请问是JUnit还是TestNG等?

预先感谢您的输入。您的假设是正确的,为域模型进行单元测试非常重要,以便每次数据库更改时都运行它们以及实体。
我使用类似的过程来测试JPA实体,实现了创建用于测试默认设计的CRUD功能的JUnit测试的基本框架。
通过调用DAO控制器的函数来运行测试,以便测试这些测试,因为这些函数还取决于实体的结构及其关系。
然后还添加了与特定模型和逻辑相关的测试,例如一个不允许级联删除的主 - 关系关系,或者只允许在满足条件时发生某些事情等。



在您的检查列表中,也在第5点添加其他验证检查,即不可空字段,唯一约束等。

JUnit仅被提及,因为我成功地使用了它,nunit,testng或任何其他单元测试框架都可以被使用。


What can go wrong?

I have started writing hibernate domain classes for my new J2EE project and during my initial analysis I felt that the following things can go wrong in domain classes that warrants me to have automated tests written for them to capture them in advance:

  1. Attribute/column mappings – I might by mistake configure a wrong column to a given attribute in the domain class
  2. I might change the database column name(s) and forget to change the corresponding mapping(s) in the domain class file
  3. I might forget to set the object attribute in the constructor. This can happen during the class creation time or while testing something by commenting out some attributes but forgetting to un-comment them back. (I have done this mistake in real life).
  4. I have NOT defined the attributes with the correct data types for the corresponding database column types.
  5. Boundary conditions testing to ensure that we have the right sized data types defined for the attributes. This will also capture any inadvertent changes to the data types in the future, say from long to int.

Whether testing a domain class against these changes is called unit testing or not, I am not really sure as it involved database while testing. What I need is a way to test my domain classes to ensure that they are NOT broken due to any such above mentioned changes, inadvertently, of-course.

Important Assumption: The database entities (tables, constraints, indexes) have been created accurately and there are no errors in them, as in, the data types, size etc. have been defined accurately. In other words, as part of the ‘(unit) testing of the domain classes’, we won’t be testing the schema for potential errors. Examples below: * Data types and their sizes are correct * If required constraints (PK, FK, UK, indexes etc.) are present and they are correct

Automated testing of the domain classes

For issues 1, 2, 4 - Use: validate in hibernate configuration file. A simple test to save a record (just mandatory fields is enough) into the table(s) concerned will ensure that these issues are not present in the mappings. Assertion: There should not be any exception thrown.

For issue 3 – A simple test to save a record with all the fields will capture if there is any field left out. Assertion: There should not be any exception thrown.

For issue 5 – Test case is needed that will insert a record with all the fields with max boundary conditions. Assertion: There should not be any exception thrown.

Summary: A test case to save a record with all the fields populated with max boundary values will be good enough to test the hibernate domain classes. Assertion: There should not be any exception thrown.

Questions

  1. Does the above argument make sense? If not, why?
  2. If it makes sense, what is the right technology for the actual implementation? Would that be JUnit or TestNG, etc.?

Thanks in advance for your inputs.

解决方案

Your assumptions are correct, it is important to have unit tests for the domain model, in order to run them every time the database changes as well as the entities. I am using similar procedure for testing JPA entities, having implemented the base framework of creating JUnit tests for testing the default designed CRUD functionality. The tests are run by calling the functions of the DAO controllers, in order to test those as well, since these functions also depend on the structure of the entities and their relationships. Then tests related to the specific model and logic are also added e.g. a master-detail relationship where cascade delete is not allowed, or only allow something to happen if a condition is met etc.

To your list of checks, at point 5 also add other validation checks i.e. not nullable fields, unique constraints etc.

JUnit has only been mentioned because i have used it successfully, nunit, testng or any other unit testing framework could be used.

这篇关于在hibernate领域类中会出现什么问题 - 这样我们需要(单元)测试它们吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆