<p>I've been trying to implement unit testing and currently have some code that does the following:</p>

<ol>
<li>query external database, loading
into a feed table </li>
<li>query a view,
which is a delta of my feed and data
tables, updating data table to match
feed table</li>
</ol>

<p>my unit testing strategy is this:</p>

<p>I have a testing database that I am free to manipulate.</p>

<ol>
<li>in setUP(), load some data into my testing db</li>
<li>run my code, using my testing db as the source</li>
<li>inspect the data table, checking for counts and the existence/non existence of certain records</li>
<li>clear testing db, loading in a different set of data</li>
<li>run code again</li>
<li>inspect data table again</li>
</ol>

<p>Obviously I have the data sets that I load into the source db set up such that I know certain records should be added,deleted,updated, etc.</p>

<p>It seems like this is a bit cumbersome and there should be an easier way?  any suggestions?</p>

[{'answer_id': 47534, 'author': 'Brian Matthews', 'author_id': 1969, 'author_profile': 'https://Stackoverflow.com/users/1969', 'pm_score': 3, 'selected': False, 'text': '<p><a href="http://dbunit.sourceforge.net" rel="noreferrer">DbUnit</a> will meet your needs. One thing to watch out for is that they have switched to using SLF4J as their logging facade instead of JCL. You can configure SLF4J to forward the logging to JCL but be warned if you are using Maven DbUnit sucks in their Nop log provider by default so you will have to use an exclusion, I <a href="http://btmatthews.com/blog/?p=5" rel="noreferrer">blogged</a> about this conflict recently.</p>\n'}, {'answer_id': 47540, 'author': 'David Carlson', 'author_id': 4901, 'author_profile': 'https://Stackoverflow.com/users/4901', 'pm_score': 4, 'selected': True, 'text': '<p>Is it your intent to test the view which generates the deltas, or to test that your code correctly adds, deletes and updates in response to the view?</p>\n\n<p>If you want to test the view, you could use a tool like <a href="http://dbunit.sourceforge.net/" rel="noreferrer">DBUnit</a> to populate your feed and data tables with various data whose delta you\'ve manually calculated.  Then, for each test  you would verify that the view returns a matching set.</p>\n\n<p>If you want to test how your code responds to diffs detected by the view, I would try to abstract away database access.  I imagine an java method to which you can pass a result set (or list of POJO/DTO\'s) and returns a list of parameter Object arrays (again, or POJO\'s) to be added.  Other methods would parse the diff list for items to be removed and updated.  You could then create a mock result set or pojo\'s, pass them to your code and verify the correct parameters are returned.  All without touching a database.</p>\n\n<p>I think the key is to break your process into parts and test each of those as independently as possible.</p>\n'}, {'answer_id': 76048, 'author': 'Aaron', 'author_id': 7659, 'author_profile': 'https://Stackoverflow.com/users/7659', 'pm_score': 1, 'selected': False, 'text': '<p>I use DbUnit, but also I work very hard to not to have to test against the DB.\nTests that go against the database should only exist for the purpose of testing the database interface.\nSo I have Mock Db Connections that I can set the data for use in all the rest of my tests.</p>\n'}, {'answer_id': 200218, 'author': 'Tatu Lahtela', 'author_id': 26734, 'author_profile': 'https://Stackoverflow.com/users/26734', 'pm_score': 1, 'selected': False, 'text': '<p>If you are using Maven, one option is to use the <a href="http://www.mojohaus.org/sql-maven-plugin/" rel="nofollow noreferrer">sql-maven-plugin</a>. It allows you to run database initialization/population scripts during the maven build cycle.</p>\n'}, {'answer_id': 252852, 'author': 'Jeroen Heijmans', 'author_id': 30748, 'author_profile': 'https://Stackoverflow.com/users/30748', 'pm_score': 1, 'selected': False, 'text': '<p>Apart from the already suggested DBUnit, you may want to look into <a href="http://unitils.org/" rel="nofollow noreferrer">Unitils</a>. It uses DBUnit, but provides more than that (quoting from the site):</p>\n\n<blockquote>\n  <ul>\n  <li>Automatic maintenance of databases, with support for incremental,\n  repeatable and post processing scripts</li>\n  <li>Automatically disable constraints and set sequences to a minimum value </li>\n  <li>Support for Oracle, Hsqldb, MySql, DB2, Postgresql, MsSql and Derby</li>\n  <li>Simplify test database connection setup</li>\n  <li>Simple insertion of test data with DBUnit  * Run tests in a transaction </li>\n  <li>JPA entity manager creation and injection for hibernate, toplink and *\n  Hibernate SessionFactory creation and\n  session </li>\n  <li>Automatically test the mapping of JPA entities / hibernate mapped\n  objects with the database</li>\n  </ul>\n</blockquote>\n'}]
['https://Stackoverflow.com/questions/47524', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/292/']