帮助编写用于 JDBC 的 JUnit
我创建了一个类,在其中将值插入到 SQL 中,如下所示:
public class ABC{
some code here..........
...............
public void insertUsers(String firstName,String lastName,String location){
pre.setString(1,firstName);
我为该类创建了测试类。
我想使用断言语句为此方法 insertUsers() 编写测试用例。 如何为上述方法编写断言语句。
I created one class,in which i am inserting values into SQL as follows:
public class ABC{
some code here..........
...............
public void insertUsers(String firstName,String lastName,String location){
pre.setString(1,firstName);
I created test class for this class.
I want to write test case for this method insertUsers(),using assert statement.
how to write assert statement for above method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在进行单元测试时,应避免访问外部资源,例如数据库、文件系统、网络等。这是为了将测试保持在内存中(快速),同时也与外部故障隔离。您只想测试类中某些功能的特定部分,而不是其他。
这对您来说意味着需要模拟
conn
变量(我假设是数据库连接)。您可以使用依赖注入之类的方法轻松完成此操作,这意味着您在构建类时将一些东西传递到你的类中。在这种情况下,您将传入一个具有conn
使用的必要函数的接口。然后在生产中,您传递真实的数据库连接对象,而在测试中,您传递您控制的模拟。因此,您可以使用
conn
检查ABC
调用并执行您期望的操作。您正在使用的pre
也是如此。你可以这样看:我想测试类
ABC
,为了做到这一点,我需要看看它如何使用pre
和conn,所以我用我自己的测试实现替换它们,我可以在使用
ABC
执行某些操作后进行检查。为了专门帮助您完成正在做的事情,您需要展示
pre
是什么,并告诉我们您打算测试什么。When doing unit testing one should avoid accessing external resources such as databases, filesystems, network etc. This is to keep the tests in memory (fast), but also isolated from external failures. You only want to test a specific part of some functionality in e.g. a class, nothing else.
What this means for you is that the
conn
variable (I assume is the db connection) needs to be mocked out. You can do this easily with something like dependency injection, which means you pass in things into your class when constructing it. In this case you would pass in an interface which has the necessary functionsconn
uses.Then in production you pass in the real db connection object while in test you pass in a mock which you control. Hence, you can then check that
ABC
calls and does what you expect it to do withconn
. The same goes forpre
you're using.You can see it like this: I would like to test class
ABC
, and in order to do that I need to see how it usespre
andconn
, so I replace those with my own test implementations I can check after doing something withABC
.In order to specifically help you with what you're doing you need to show what
pre
is and tell us what you intend to test.好吧,如果您确实想测试更新数据库,您可以这样做。通常人们遵循以下两种方法之一 -
使用 Spring AbstractTransactionalDataSourceSpringContextTests 这允许您向数据库添加任何值,然后 spring 将处理并恢复您插入的值。
使用单独的数据库仅用于您的 JUnit 测试。你真的不需要任何沉重的东西。您可以使用诸如 HSQLDB 之类的东西,它实际上是一个轻量级 java 数据库。这将允许您从生产/QA 数据库中获得单独的测试数据。
完成上述操作后(并且您已运行插入语句),只需从 JUnit 运行 select 语句即可获取数据,然后将先前的数据与实际数据进行比较。
Well if you really want to test updating your database you can do that. Usually people follow one of the below two approaches -
Use Spring AbstractTransactionalDataSourceSpringContextTests This allows you to add any values to the database and then spring will take care and revert the values that you have inserted.
Use a seperate database Just for your JUnit tests. You really dont need anything heavy. You can use something like the HSQLDB which is really a lightweight java database. This will allow you to have separate test data from your production/QA database.
After the above is done(and you have run the insert statement) simply run select statement from your JUnit to get the data and then compare the previous data with the actual data.
几句话。
我只会在开发期间使用标准
assert
。它将检查条件并在条件计算结果为 false 时抛出运行时异常。如果您期望非法参数,那么最好在方法中添加一些“正常”代码来处理这些值或抛出
IllegalArgumenException
并写入日志条目。不要在此方法中关闭连接!仅当您以相同的方法打开/创建连接时才执行此操作。在较大的应用程序中,您将无法在一段时间后找出谁关闭了连接。如果
insertUsers
的调用者打开了连接,则调用者应该自行关闭它!(如果您告诉我们您到底想要测试什么,可以提供更多帮助 - 方法参数或插入是否成功)
A couple of remarks.
I'd use the standard
assert
during development only. It will check a condition and throw a runtime exception, if the condition evaluates to false.If you expect illegal arguments, than it's much better to add some "normal" code to the method to handle those values or throw an
IllegalArgumenException
and write log entry.Do not close the connection in this method! Do it only when you open/create the connection in the very same method. In larger applications you won't be able find out who closed the connection after a while. If the caller of
insertUsers
opened the connection, the caller should close it itself!(more help possible if you tell us what exactly you want to test - the method parameters or if the insert was a success)
我不会测试将数据插入数据库,实际上在单元测试期间访问数据库的性能不高,这可以通过应用程序的自动化功能 GUI 测试工具来覆盖。
您可能想要测试的是预期查询的生成,如果您将语句的生成和执行分开,则可以实现这一点,您将能够将生成的语句与预期的语句进行比较,而无需从unitest访问数据库。
I wouldnt test the insertion of the data to the database, actually its not performant to access database during unittesting, this can be covered threw automated functional GUI testing tools of your application.
what you may want to test is the generation of the expected queries, this can realised if you seperate the geenration and the execution of the statements, you will be able to compare generated statements with expected ones without having to access you database from the unitest.