春季如何在单元测试中插入记录(没有删除方法) [英] How unit test insert record in spring (no delete method)

查看:81
本文介绍了春季如何在单元测试中插入记录(没有删除方法)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Spring的jdbcTemplate和创建读取更新(不删除)操作来进行DAO操作.

I have DAO using Spring's jdbcTemplate with Create Read Update (no Delete) operation.

创建方法具有ID参数,该参数是表中的唯一键.

Create method have ID parameter which is unique key in table.

除了模拟DAO,我如何才能在不违反约束的情况下实际测试创建?

Except mocking DAO, how can I actually test create without getting constraint violation?

使用随机ID有时仍会失败

Using random ID still can fail sometimes

我应该重写setAutoCommit以避免添加记录吗?仍然认为有效的单元测试吗?

Should I override setAutoCommit to avoid adding record? is it still consider a valid unit test?

我必须事先在SQL中删除数据库中的记录,还是这种类型的测试有spring选项?

Must I delete in SQL the record in database beforehand or is there spring option for this types of tests?

还是应该将其视为集成测试而不是单元测试?

Or should I consider it as integration test and not unit test?

编辑

我正在使用Oracle,我无法使用序列来为ID创建值

I'm using Oracle, I can't use sequence for creating values for the ID

我们在生产中存在一些数据源(不用于测试)

We have a few Data sources exists (not for testing) in production

推荐答案

这实际上取决于这种测试的目的是什么,并不是所有测试在这方面都是单元测试".

It really depends on what is the purpose of such a test, not all the tests are "unit tests" with this respect.

例如,如果目标是测试封装业务逻辑的服务",但是有时从此服务中调用DAO,则最好的方法可能就是按照您的建议模拟DAO. 在这种情况下,该测试显然不会涵盖DAO,但是该服务将涵盖.

If, for example, the goal is to test the "service" that encapsulates a business logic, but from this service, sometimes there are calls to DAO, then probably the best way is to just mock the DAO as you suggest. In this case, DAO obviously won't be covered by this test, but the service will.

如果目的是测试SQL语句(并且我假设DAO除了SQL语句外不包含任何内容,也许将它们转换为域对象),那么就不能选择模拟.

If the purpose is to test SQL statements (and I assume that DAO contains nothing but SQL statements + maybe transforming them into the domain object), then mocking is not an option.

在这种情况下,测试应该包括对某种数据库的调用,但是在这种情况下,它不再称为单元测试(单元测试运行起来非常快,并且仅在内存中运行,没有DB,没有I/O等),我将其称为集成测试(也如您所建议),但是对于这种测试,不同的人可能会有不同的名字.

In this case, the test should include calls to some kind of database, but in this case, it's not called a unit test anymore (a unit test is something that runs really fast and only in memory, no DBs, no I/O, etc.) I'll call this an integration test (as you also suggest), but different people have probably different names for this kind of tests.

实际上,我们需要两种测试,因为它们测试的是不同的东西

In practice, we need both kinds of tests because they test different things

那么,如何测试呢?

首先应该做出决定,应该使用哪个数据库,这里有3种方法:

First of all the decision should be made, which database should be used, there are 3 approaches here:

  1. 运行一个真实的数据库,由用户共享,测试假定其已预先安装
  2. 使用内存数据库运行
  3. 在测试套件运行时运行数据库的DB docker映像,然后销毁它

关于哪种方法更好的讨论本身非常有趣,但对于IMO这个问题而言,却超出了范围,每个选择都有其含义.

While the discussion of which approach is better is very interesting on its own, its out of scope for this question IMO, each choise has its implications.

完成此决定后,您应该从代码中决定如何使用此数据库.

Once you're done with this decision, you should decide how to work with this database from the code.

通常,弹簧测试使用以下模式:

Usually spring tests use the following pattern:

  1. 在测试前打开交易
  2. 运行测试(更改数据,甚至更改架构-如果需要,添加列,表).做断言
  3. 无论测试结果如何,都应回滚事务,以便数据将像测试前一样

因此,如果您对所有测试都遵循这种方法,那么它们将从空"数据状态开始,这样就不会出现任何违反约束的情况 这也有效地解决了删除记录"问题,因为在回滚事务时,无论如何都会删除数据.

So if you follow this approach for all your tests, they'll start with "empty" data state so that no constraint violations are expected This effectively solves also the "deletion of the record" question because the data will be deleted anyway when the transactions is rolled back.

现在要删除交易外的记录.

Now regarding the Deletion of the record outside a transaction.

一种明显的方法是直接从测试(在DAO外部)执行删除的sql,以便DAO(不会更改生产代码)

An obvious approach is to execute an sql of deletion right from the test (outside the DAO), so that DAO (a production code won't be changed)

您可以直接将DataSource/JDBCTemplate注入到测试中(Spring测试完全支持此操作),然后从那里调用所需的SQL

You can inject DataSource/JDBCTemplate right into the test (Spring test perfectly supports this) and call the required SQL from there

这篇关于春季如何在单元测试中插入记录(没有删除方法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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