使用 MongoDB 进行单元测试 [英] Unit testing with MongoDB

查看:38
本文介绍了使用 MongoDB 进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我选择的数据库是 MongoDB.我正在编写一个数据层 API 来从客户端应用程序中抽象出实现细节——也就是说,我本质上提供了一个公共接口(一个充当 IDL 的对象).

My database of choice is MongoDB. I'm writing a data-layer API to abstract implementation details from client applications - that is, I'm essentially providing a single public interface (an object which acts as an IDL).

我正在以 TDD 方式测试我的逻辑.在每次单元测试之前,都会调用 @Before 方法来创建一个数据库单例,之后,当测试完成时,会调用 @After 方法来删​​除数据库.这有助于促进单元测试之间的独立性.

I'm testing my logic as I go in a TDD manner. Before each unit test, an @Before method is called to create a database singleton, after which, when the test completes, an @After method is called to drop the database. This helps to promote independence among unit tests.

几乎所有的单元测试,即执行上下文查询,都需要事先进行某种插入逻辑.我的公共接口提供了一个插入方法 - 然而,将此方法用作每个单元测试的前导逻辑似乎是不正确的.

Nearly all unit tests, i.e. performing a contextual query, require some kind of insertion logic to occur before hand. My public interface provides an insert method - yet, it seems incorrect to use this method as precursor logic to each unit test.

我确实需要某种模拟机制,但是,我对模拟框架没有太多经验,而且似乎 Google 没有返回任何可能与 MongoDB 一起使用的模拟框架.

Really I need some kind of mocking mechanism, yet, I haven't had much experience with mocking frameworks, and it seems that Google returns nothing re a mocking framework one might use with MongoDB.

其他人在这些情况下会怎么做?也就是说,人们如何对与数据库交互的代码进行单元测试?

What do others do in these situations? That is, how do people unit test code that interacts with a database?

此外,我的公共接口连接到外部配置文件中定义的数据库 - 使用此连接进行单元测试似乎不正确 - 同样,这种情况会从某种模拟中受益吗?

Also, my public interface connects to a database defined in a external configuration file - it seems incorrect to use this connection for my unit testing - again, a situation that would benefit from some kind of mocking?

推荐答案

正如 sbridges 在这篇文章中所写的那样,没有一个专门的服务(有时也称为存储库或 DAO)来抽象数据访问是一个坏主意.逻辑.然后您可以通过提供 DAO 的模拟来测试逻辑.

As sbridges wrote in this post it is a bad idea not to have a dedicated service (sometimes also known as repository or DAO) which abstracts the data access from the logic. Then you could test the logic by providing a mock of the DAO.

我做的另一种方法是创建 Mongo 对象的 Mock(例如 PowerMockito),然后返回适当的结果.这是因为您不必在单元测试中测试数据库是否工作,但更重要的是,您应该测试是否将正确的查询发送到数据库.

Another approach which I do is to create a Mock of the Mongo object (e.g. PowerMockito) and then return the appropriate results. This because you don't have to test if the database works in unit tests but more over you should test if the right query was sent to the databse.

Mongo mongo = PowerMockito.mock(Mongo.class);
DB db = PowerMockito.mock(DB.class);
DBCollection dbCollection = PowerMockito.mock(DBCollection.class);

PowerMockito.when(mongo.getDB("foo")).thenReturn(db);
PowerMockito.when(db.getCollection("bar")).thenReturn(dbCollection);

MyService svc = new MyService(mongo); // Use some kind of dependency injection
svc.getObjectById(1);

PowerMockito.verify(dbCollection).findOne(new BasicDBObject("_id", 1));

那也是一种选择.当然,模拟的创建和适当对象的返回只是作为上面的示例进行编码.

That would also be an option. Of course the creation of the mocks and returning of the appropriate objects is just coded as an example above.

这篇关于使用 MongoDB 进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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