简体   繁体   English

涉及数据库的单元测试(插入/更新/选择)

[英]Unit Testing involving DB (Insert/Update/Select)

Looking forward to know the best practice in this area and context.期待了解该领域和背景下的最佳实践。

Two scenarios两种场景

  1. I have a component written in Java & Spring where I am getting some data , converting it into another format that involves core business logic and insert/update a Cassandra DB.我有一个用 Java 和 Spring 编写的组件,我在其中获取一些数据,将其转换为涉及核心业务逻辑和插入/更新 Cassandra DB 的另一种格式。
  2. Another java component reads data from that DB, runs business logic on that and take care of some other functionalities.另一个 java 组件从该数据库读取数据,在其上运行业务逻辑并处理一些其他功能。

Now while writing the unit test cases I can think of the below two high level approaches现在在编写单元测试用例时,我可以想到以下两种高级方法

  1. I can use Mockito or similar and mock the DAO object so that I don't actually call DB while doing get and save operations on DAO Object.我可以使用 Mockito 或类似的工具来模拟 DAO 对象,这样我就不会在对 DAO 对象执行 get 和 save 操作时实际调用 DB。
  2. I can actually not mock and let is connect to DB.我实际上不能模拟,让我们连接到数据库。
  3. We connect to DB only during build snapshot (for few test cases)我们仅在构建快照期间连接到数据库(对于少数测试用例)

However -然而 -

For (1) - We are not actually testing if the data are stored/updated properly.对于 (1) - 我们实际上并未测试数据是否正确存储/更新。 For (2) - This should not be a good idea to do it in unit / regression as we don't want to connect to DB every time we do mvn install or build For (3) - This sounds like a better option provided this is feasible.对于 (2) - 在单元/回归中执行此操作不是一个好主意,因为我们不想每次执行 mvn install 或 build For (3) 时都连接到数据库 - 这听起来是一个更好的选择是可行的。

Question is - What is the best practice around testing the DB operations as part of the unit tests or regression test or during build.问题是 - 作为单元测试或回归测试的一部分或在构建期间测试数据库操作的最佳实践是什么。 Should we always mock these calls ?我们应该总是嘲笑这些电话吗?

Your scenarios are quite abstract, therefore the answer is as well:您的场景非常抽象,因此答案也是:

The testing scenarios you describe seem to be a mixture of unit-testing and integration-testing problems.您描述的测试场景似乎是单元测试和集成测试问题的混合体。 In unit-testing you would like to test the business logic in depth (computations, but ideally no interactions with other components) with the intent to find bugs in the computational parts.在单元测试中,您希望深入测试业务逻辑(计算,但最好不要与其他组件交互),以找出计算部分中的错误。 Dependencies to other components (like the DB) are disturbing.对其他组件(如 DB)的依赖性令人不安。 In integration testing, however, you are searching for the bugs in the interaction between the different components.然而,在集成测试中,您正在寻找不同组件之间交互中的错误。

Therefore, a first step would be to re-design your component such that you separate the computation-dominated parts of the code from the interaction-dominated parts.因此,第一步是重新设计您的组件,以便将代码的计算主导部分与交互主导部分分开。 Ideally, you end up with a number of functions/methods for the computation-dominated parts that have no interactions with other components, such that you can test these functions with unit-testing but without need for mocking.理想情况下,您最终会得到许多用于计算主导部分的函数/方法,这些函数/方法与其他组件没有交互,这样您就可以使用单元测试来测试这些函数,而无需模拟。 In the not-so-ideal case, you still have some, but only few interactions left, so that at least the effort for mocking is reduced.在不太理想的情况下,您仍然有一些交互,但只剩下很少的交互,因此至少减少了模拟的工作量。

The second type of functions/methods you obtain after your re-design would be the interaction-dominated functions.重新设计后获得的第二种功能/方法将是交互主导功能。 Ideally, the interaction-dominated functions have no computational code, but only contain interactions with the other components.理想情况下,交互主导函数没有计算代码,而只包含与其他组件的交互。 Then, for these functions, unit-testing does not make sense, since the only bugs this type of code could contain are bugs like calling functions (or performing DB operations) in a wrong order, calling the wrong function (or DB operation), passing the arguments in the wrong order, passing incompatible arguments, receiving unexpected data etc., and all these bugs can not be found with unit-testing, but with integration testing.然后,对于这些函数,单元测试没有意义,因为这种类型的代码可能包含的唯一错误是诸如以错误的顺序调用函数(或执行 DB 操作)、调用错误的函数(或 DB 操作)、以错误的顺序传递参数,传递不兼容的参数,接收意外数据等,所有这些错误都无法通过单元测试找到,但可以通过集成测试找到。

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

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