在内部测试使用特定于MySQL的查询的代码的合适方法是什么 [英] What's the appropriate way to test code that uses MySQL-specific queries internally

查看:117
本文介绍了在内部测试使用特定于MySQL的查询的代码的合适方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在收集数据,并使用Java将这些数据存储在MySQL数据库中.此外,我使用Maven来构建项目,使用TestNG作为测试框架,并使用Spring-Jdbc来访问数据库.我已经实现了一个DAO层,该层封装了对数据库的访问.除了使用DAO类添加数据外,我还想执行一些查询,这些查询将数据汇总并将结果存储在其他表中(例如物化视图).

I am collecting data and store this data in a MySQL database using Java. Additionally, I use Maven for building the project, TestNG as a test framework, and Spring-Jdbc for accessing the database. I've implemented a DAO layer that encapsulates the access to the database. Besides adding data using the DAO classes I want to execute some queries which aggregate the data and store the results in some other tables (like materialized views).

现在,我想编写一些测试用例,以检查DAO类是否按预期工作.因此,我想到了使用一个内存数据库,其中将填充一些测试数据.由于我还使用特定于MySQL的SQL查询来聚合数据,因此遇到了一些麻烦:

Now, I would like to write some testcases which check whether the DAO classes are working as they should. Therefore, I thought of using an in-memory database which will be populated with some test data. Since I am also using MySQL-specific SQL queries for aggregating data, I went into some trouble:

  1. 首先,我想到了简单地使用Spring-Jdbc提供的嵌入式数据库功能来实例化嵌入式数据库.我决定使用H2实现.由于聚合查询正在使用MySQL特定的内容(例如DATE()之类的时间处理函数),因此我遇到了麻烦.这种方法的另一个缺点是,我需要维护两个ddl文件-实际的ddl文件,用于定义MySQL中的表(此处定义编码,并向表和列中添加注释,这两个功能都是特定于MySQL的);以及定义相同表但没有注释等的测试ddl文件,因为H2不支持注释.
  2. 我找到了将MySQL用作嵌入式数据库的说明,可以在测试用例中使用它(http://literatitech.blogspot.de/2011/04/embedded-mysql-server-for-junit-testing .html).对我来说,这听起来确实很有希望.不幸的是,它没有用:发生MissingResourceExcpetion找不到资源'5-0-21/Linux-amd64/mysqld'".似乎驱动程序无法在我的本地计算机上找到数据库守护程序.但是我不知道要寻找解决该问题的解决方案.

现在,我有点困惑,我想知道我是否应该以不同的方式创建体系结构.有人有一些提示,我应该如何设置适当的系统?我还有其他两个选择:

Now, I am a little bit stuck and I am wondering if I should have created the architecture differently. Do someone has some tips how I should setup an appropriate system? I have two other options in mind:

  1. 我将使用本地MySQL实例,而不是使用嵌入式数据库,并建立一个仅用于测试用例的数据库.这个选项听起来很慢.实际上,稍后我可能想设置CI服务器,并且我认为使用嵌入式数据库会更合适,因为测试运行速度更快.
  2. 我从SQL查询中删除了所有特定于MySQL的内容,并使用H2作为嵌入式数据库进行测试.如果此选项是正确的选择,我将需要找到另一种方法来测试将数据聚合到具体化视图中的SQL查询.
  3. 还是我没有想到第三个选项?

任何提示,我将不胜感激.

I would appreciate any hints.

谢谢, XComp

推荐答案

如果无法使内存MySQL数据库正常工作,我建议使用H2数据库进行简单"测试一个专用的MySQL实例,用于测试特定于MySQL的查询.

If it is not possible to get the in-memory MySQL database to work I suggest using the H2 database for the "simple" tests and a dedicated MySQL instance to test MySQL-specific queries.

此外,可以将真实MySQL数据库的测试配置为单独的Maven配置文件中的集成测试,这样它们就不会成为常规Maven构建的一部分.在CI服务器上,您可以创建一个其他作业,该作业定期运行MySQL测试,例如每天或每隔几个小时.通过这种设置,您可以保留并测试针对特定产品的查询,而常规构建不会变慢.即使测试数据库不可用,您也可以运行常规版本.

Additionally, the tests for the real MySQL database can be configured as integration tests in a separate maven profile so that they are not part of the regular maven build. On the CI server you can create an additional job that runs the MySQL tests periodically, e.g. daily or every few hours. With such a setup you can keep and test your product-specific queries while your regular build will not slow down. You can also run a normal build even if the test database is not available.

有一个不错的用于集成测试的maven插件,名为 maven-failsafe-plugin .它提供了集成前和集成后的测试步骤,可用于在测试之前设置测试数据并在测试之后清理数据库.

There is a nice maven plugin for integration tests called maven-failsafe-plugin. It provides pre- and post- integration test steps that can be used to setup the test data before the tests and to cleanup the database after the tests.

这篇关于在内部测试使用特定于MySQL的查询的代码的合适方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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