如何模拟用于测试的数据库(Java)? [英] How to simulate a DB for testing (Java)?

查看:310
本文介绍了如何模拟用于测试的数据库(Java)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java编程,我的应用程序正在做很多使用DB。因此,重要的是我能够轻松地测试我的数据库使用。

什么数据库测试是关于?对我来说,他们应该提供两个简单的要求:

I'm programming in Java and my applications are making a lot of use of DB. Hence, it is important for me to be able to test my DB usage easily.
What DB tests are all about? For me, they should supply two simple requirements:


  1. 验证SQL语法。

  2. 更重要的是,根据给定的情况检查数据是否被正确选择/更新/插入。

好吧,我需要的只是一个数据库。

但实际上,因为使用DB进行测试几乎没有什么困难:

Well then, it seems that all I need is a DB.
But actually, I prefer not, as there are few difficulties using a DB for a test:


  • 只是给自己一个测试DB,它有多难? - 好吧,在我的工作地点,有一个个人测试DB是很不可能的。您必须使用可供所有人使用的公开数据库。

  • 这些测试肯定不是很快... - 数据库测试往往比平时测试慢。

  • 这个程序应该处理任何情况! - 它变得有点恼人,甚至不可能尝试和模拟每个案例在一个数据库。对于每种情况,应该进行一定量的插入/更新查询,这是烦人的,需要时间。

  • 等一下,你怎么知道这个表中有542行? 。 - 测试的主要原则之一是能够以不同于测试代码的方式测试功能。使用数据库时,通常有一种方法可以执行某些操作,因此测试与核心代码完全相同。

  • "Just get yourself a testing DB, how hard could it be?" - Well, in my working place, to have a personal testing DB is pretty impossible. You have to use a "public" DB, which is accessible for everyone.
  • "These tests sure ain't fast..." - DB tests tend to be slower than usual tests. It's really not ideal to have slow tests.
  • "This program should handle any case!" - It becomes somewhat annoying and even impossible to try and simulate each and every case in a DB. For each case a certain amount of insert/update queries should be made, which is annoying and takes time.
  • "Wait a second, how do you know there are 542 rows in that table?" - One of the main principles in testing, is to be able to test the functionality in a way different from that of your tested-code. When using a DB, there's usually one way to do something, therefore the test is exactly the same as the core-code.

,你可以弄清楚我不喜欢DBs,当它涉及到测试(当然,我会得到到这一点,但我宁愿到那里我的测试,后我发现大多数错误使用休息的测试方法)。但我正在寻找什么?

So, you can figure out I don't like DBs when it comes to tests (of course I will have to get to this in some point, but I'd rather get there later on my testing, after I found most bugs using the rest of the test methods). But what am I looking for?

我正在寻找一种方法来模拟一个DB,一个模拟DB,使用文件系统或只是虚拟内存。我认为也许有一个Java工具/包允许简单地构建(使用代码接口)DB模拟每个测试,模拟表和行,SQL验证,并与代码接口监视其状态(而不是使用SQL )。

I'm looking for a way to simulate a DB, a mock DB, using the file system or just virtual memory. I thought that maybe there's a Java tool/package which allows to simply construct (using code interface) a DB mock per test, with simulated tables and rows, with SQL verification, and with a code interface for monitoring its status (rather then using SQL).

您熟悉这种工具吗?

修改:感谢您的回答!虽然我要求一个工具,你也提供了一些提示,关于这个问题:)我需要一些时间检查你的优惠,所以我不能说现在是否你的答案是不满意。

Thanks for the answers! Although I was asking for a tool, you also provided me with some tips concerning the problem :) It will take me some time to check out your offers, so I can't say right now whether your answers were satisfying not.

无论如何,这里有一个更好的视图我想要的 - 想象一个名为DBMonitor的类,它的一个功能是找到表中的行数。下面是一个想象的代码,我如何使用JUnit测试该功能:

Anyway, here's a better view of what I'm looking for - Imagine a class named DBMonitor, that one of its features is finding the number of rows in a table. Here is an imaginary code of how I would like to test that feature using JUnit:

public class TestDBMonitor extends TestCase {

    @Override
    public void setUp() throws Exception {

       MockConnection connection = new MockConnection();

       this.tableName = "table1";
       MockTable table = new MockTable(tableName);

       String columnName = "column1";
       ColumnType columnType = ColumnType.NUMBER;
       int columnSize = 50;
       MockColumn column = new MockColumn(columnName, columnType, columnSize);
       table.addColumn(column);

       for (int i = 0; i < 20; i++) {
           HashMap<MockColumn, Object> fields = new HashMap<MockColumn, Object>();
           fields.put(column, i);
           table.addRow(fields);
       }

       this.connection = connection;
    }

    @Test
    public void testGatherStatistics() throws Exception {

       DBMonitor monitor = new DBMonitor(connection);
       monitor.gatherStatistics();
       assertEquals(((MockConnection) connection).getNumberOfRows(tableName),
                    monitor.getNumberOfRows(tableName));
    }

    String tableName;
    Connection connection;
}



我希望这段代码足够清楚我的想法错误,我手动输入没有我亲爱的Eclipse:P)。

I hope this code is clear enough to understand my idea (excuse me for syntax errors, I was typing manually without my dear Eclipse :P).

顺便说一句,我使用ORM部分,我的原始SQL查询是相当简单,不应该从一个平台到另一个平台。

By the way, I use ORM partially, and my raw SQL queries are quite simple and shouldn't differ from one platform to another.

推荐答案

Java随附 Java DB

也就是说,我建议不要使用不同类型的数据库在生产中使用,除非您通过ORM层。否则,您的SQL可能不是您认为的跨平台。

That said, I would advise against using a different type of DB than what you use in production unless you go through an ORM layer. Otherwise, your SQL might not be as cross-platform as you think.

还可以查看 DbUnit

这篇关于如何模拟用于测试的数据库(Java)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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