title: Database data needed in integration tests; created by API calls or using imported data?
body: <p>This question is more or less programming language agnostic. However as I'm mostly into Java these days that's where I'll draw my examples from. I'm also thinking about the OOP case, so if you want to test a method you need an instance of that methods class.</p>
  
  <p>A <em>core rule</em> for <strong><a href="http://en.wikipedia.org/wiki/Unit_testing" rel="noreferrer">unit tests</a></strong> is that they should be autonomous, and that can be achieved by isolating a class from its dependencies. There are several ways to do it and it depends on if you inject your dependencies using <a href="http://en.wikipedia.org/wiki/Inversion_of_control" rel="noreferrer">IoC</a> (in the Java world we have Spring, EJB3 and other frameworks/platforms which provide injection capabilities) and/or if you mock objects (for Java you have <a href="http://www.jmock.org/" rel="noreferrer">JMock</a> and <a href="http://www.easymock.org/" rel="noreferrer">EasyMock</a>) to separate a class being tested from its dependencies.</p>
  
  <p>If we need to test groups of methods in different classes* and see that they are well integration, we write <strong><a href="http://en.wikipedia.org/wiki/Integration_testing" rel="noreferrer">integration tests</a></strong>. And here is my question!</p>
  
  <ul>
  <li>At least in web applications, state is often persisted to a database. We could use the same tools as for unit tests to achieve independence from the database. But in my humble opinion I think that there are cases when not using a database for integration tests is mocking too much (but feel free to disagree; not using a database at all, ever, is also a valid answer as it makes the question irrelevant).</li>
  <li><strong>When you use a database for integration tests, how do you fill that database with data?</strong> I can see two approaches:
  
  <ul>
  <li>Store the database contents for the integration test and load it before starting the test. If it's stored as an SQL dump, a database file, XML or something else would be interesting to know.</li>
  <li>Create the necessary database structures by API calls. These calls are probably split up into several methods in your test code and each of these methods <em>may</em> fail. It could be seen as your integration test having dependencies on other tests.</li>
  </ul></li>
  </ul>
  
  <p>How are you making certain that database data needed for tests is there when you need it? And why did you choose the method you choose?</p>
  
  <p><strong>Please provide an answer with a motivation</strong>, as it's in the motivation the interesting part lies. Remember that just saying "It's best practice!" isn't a <em>real</em> motivation, it's just re-iterating something you've read or heard from someone. For that case please explain <em>why</em> it's best practice.</p>
  
  <p>*I'm including one method calling other methods in (the same or other) instances of the same class in my definition of unit test, even though it might technically not be correct. Feel free to correct me, but let's keep it as a side issue.</p>