简体   繁体   English

通过JUnit DAO类进行测试

[英]Test via JUnit DAO class

I have a DAO class in which I need to test method called getItemById() which returns Item object from DB's table. 我有一个DAO类,我需要在其中测试名为getItemById()的方法,该方法从DB的表中返回Item对象。

As long as I understand I have to make an Item object in that test and check if it equals to returned from method? 据我了解,我必须在该测试中创建一个Item对象,并检查它是否等于从方法返回的值? Or I have to just check if it returns an Item object? 还是我只需要检查它是否返回Item对象?

What if table is empty or no row with that id at all? 如果表为空或根本没有该ID的行怎么办?

Sorry, this is a quite newbie question, but I can't make it clear in my head. 抱歉,这是一个相当新手的问题,但我不清楚。 Please help! 请帮忙!

Running tests against a database where you can't predict what's in it is not effective; 在无法预测数据库内容的数据库上运行测试是无效的。 any test that is resilient enough to accommodate changing data is going to be worthless for the purpose of confirming whether the code under test actually does the right thing. 为了确认被测代码是否确实做正确的事,任何具有足够弹性以容纳变化数据的测试都将一文不值。 I would make the test use its own database instance, so that there's no question of interference from other users mucking up my test, or my test changing data out from under somebody else. 我将使测试使用其自己的数据库实例,这样就不会受到其他用户干扰我的测试的干扰,也不会因为我的测试从其他人那里更改数据而产生干扰。 The ideal choice would be an in-memory database like H2 , that the test can instantiate and throw away when it's done with it. 理想的选择是像H2这样的内存数据库,测试完成后可以实例化并丢弃。 That way the test can run anywhere (for instance on a CI server), with the same results. 这样,测试可以在任何地方(例如,在CI服务器上)运行,并获得相同的结果。

The test needs to run the ddl to create the schema and populate the database before executing. 该测试需要运行ddl来创建模式并在执行之前填充数据库。 There are different tools you can use for this. 您可以为此使用不同的工具。 DbUnit is popular, there is also an alternative called DBSetup which is supposed to be less complicated. DbUnit很流行,还有一个名为DBSetup的替代方法,它应该不太复杂。 You can have separate test data for different scenarios. 您可以针对不同的场景使用单独的测试数据。 DbUnit has tools to extract data from a database to make it easier to create your test data. DbUnit具有从数据库提取数据的工具,使创建测试数据更加容易。

Since the database is under your control and you can populate it as you wish, you should verify that the returned object's fields are what you expect based on the populated data. 由于数据库处于您的控制之下,并且您可以根据需要填充它,因此您应该验证返回的对象的字段是否正是您期望的基于填充数据的字段。 Make the test as specific as possible. 使测试尽可能具体。

For testing the SQL and how the object is mapped to the resultset it makes sense to use a database. 为了测试SQL以及如何将对象映射到结果集,使用数据库是有意义的。 For some parts of this it would make sense to use a unit test that doesn't touch the database and uses mocks. 在某些情况下,使用不涉及数据库并使用模拟的单元测试将是有意义的。 For instance, it would be good to confirm that the connection gets closed in all cases, it's easier to use mocks than it is to cause a SQLException in your code. 例如,最好确认连接在所有情况下都已关闭,使用模拟比在代码中引起SQLException容易。

Testing using mocks would be easier if the DBConnection class was injected instead of being instantiated within the method. 如果注入DBConnection类而不是在方法中实例化,则使用模拟进行测试会更容易。 If you changed the code to inject the DBConnection then you could write a unit test (one using mocks that doesn't use a database) that checks whether the connection gets closed. 如果更改了代码以注入DBConnection,则可以编写一个单元测试(使用不使用数据库的模拟程序进行测试),以检查连接是否关闭。

To perform unit test you should walk by three steps: 要执行单元测试,您应该遵循三个步骤:

  1. Prepare test environement (eg. populate db with known test data)- so you wont ask is the table empty or not etc. 准备测试环境(例如,用已知的测试数据填充数据库)-这样您就不会问表是否为空等。
  2. Perform test and assert result 执行测试并确认结果
  3. Do cleanup - so test wont have influence on other tests 进行清理-因此测试不会影响其他测试

Besides, you should test all scenarios cuz you sholuld handle all of them 此外,您应该测试所有方案,因为您应该处理所有方案

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

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