简体   繁体   English

数据库单元测试(MySQL)

[英]Database unit testing (MySQL)

I am trying to find a way to unit test my MySQL dependant code. 我试图找到一种方法来对我的MySQL依赖代码进行单元测试。 I know how I would like it to work but cannot find the solution that would work for me. 我知道我希望它如何工作,但找不到适合我的解决方案。 I have looked into DBUnit, but it would seem (if I am not mistaken) that this would require a running database and just aids with the unit testing side of things. 我已经研究过DBUnit,但是(如果我没有记错的话)这似乎需要一个正在运行的数据库,并且只是有助于进行单元测试。 I would like some way to avoid running a mysql database when testing. 我想通过某种方式避免在测试时运行mysql数据库。 What would work great would be some sort of MySQL spoof driver that actually stored data in memory, rather than needing to access a real persistent database. 最好的是某种MySQL欺骗驱动程序,该驱动程序实际上将数据存储在内存中,而不需要访问真正的持久数据库。

In my code it is hard coded to access a MySQL database so I can't just inject some mock object. 在我的代码中,很难对访问MySQL数据库进行编码,因此我不能只注入一些模拟对象。 The way I would like it to work is that when my code calls: 我希望它的工作方式是当我的代码调用时:

DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);

It actually gets some other local database that can either be configured via maven or in the setUp of the maven test. 它实际上获得了一些其他本地数据库,这些本地数据库可以通过maven或在maven测试的setUp中进行配置。 I have looked into memory based databases such as HSQLDB but can't find a way for it to spoof the MySQL driver. 我已经研究了基于内存的数据库(例如HSQLDB),但找不到用于欺骗MySQL驱动程序的方法。

Are there any tools that provide what I am looking for? 有什么工具可以满足我的需求? Do you have any good methods for testing MySQL dependant code? 您有什么好的方法可以测试依赖于MySQL的代码?

I have had several projects in which I had to do integrations test against a running MySql server. 我有几个项目必须针对正在运行的MySql服务器进行集成测试。 Instead of spending time setting it up every time, I developed a library that sets up a local running instance of MySQL every time you run your tests. 我开发了一个 ,该在每次运行测试时都设置一个本地运行的MySQL实例,而不是每次都花时间设置它。

With that you get a test database that acts like the real thing (because it is) without having to set it up. 这样,您就可以建立一个测试数据库,该数据库的行为类似于真实事物(因为确实如此),而无需进行设置。

DBUnit is also a good alternative if you want to mock the database integration (as far as I know, there is no need for a real MySql server when using DBUnit). 如果要模拟数据库集成,DBUnit也是一个很好的选择(据我所知,使用DBUnit时不需要真正的MySql服务器)。

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

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