简体   繁体   English

模拟存储过程

[英]Mocking a StoredProcedure

I am soon writing a unit test to a service tier-class that relies heavily to org.springframework.jdbc.object.StoredProcedure class. 我即将在很大程度上依赖org.springframework.jdbc.object.StoredProcedure类的服务层类中编写单元测试。 The service object is going to be simple batch class, reading data from a stored-procedure, aggregating it and sending it to a another stored-procedure. 服务对象将是简单的批处理类,从存储过程中读取数据,对其进行聚合并将其发送到另一个存储过程。

What would be a best practice regarding such an unit test (since it relies heavily to a database). 关于这种单元测试的最佳实践是什么(因为它很大程度上依赖于数据库)。 Should I use a fake object (set of method stubs), write a mock with a database backend or mirror the real thing (with the cost of keeping the infrastructure up to date)? 我应该使用伪造的对象(方法存根集),使用数据库后端编写模拟对象还是镜像真实对象(以保持基础结构为最新状态的代价)?

I want be as lazy as possible and still guarantee quality. 我想尽可能地懒惰,并且仍然保证质量。

Personally, I would do the unit tests in a case like this with an embedded database like H2 . 就个人而言,我将在像H2这样的嵌入式数据库中进行这种情况下的单元测试。

Hope this helps. 希望这可以帮助。

I would not worry about testing end-to-end in this case. 在这种情况下,我无需担心端到端测试。 I would stub out the calls to the stored proc and make sure your code works as expected. 我将对存储的proc的调用存根,并确保您的代码按预期工作。

The next thing to do is test the stored proc. 接下来要做的就是测试存储的过程。 If you are on an oracle platform then you can test-drive the proc using ruby-plsql. 如果您在oracle平台上,则可以使用ruby-plsql测试驱动程序。 I have used this on a project before and it works well. 我以前在项目上使用过它,并且效果很好。 If you are not on an oracle platform then you are going to have to build something in your test harness that can call the stored proc in such ways that you will see the different outcomes. 如果您不在oracle平台上,那么您将不得不在测试工具中构建一些东西,以某种方式调用存储的proc,这样您将看到不同的结果。 Just shoot for the happy path and a few edge cases. 只是为幸福的道路和一些边缘情况而努力。

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

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