I must test some Thrift services using Junit. After I run my tests like a Thrift client, the help customize the server database. I'm not able to locate a good solution which could cleanup the database after each test operates. Cleanup is essential especially since the IDs have to be unique that are presently read form an XML file. Now, I must by hand alter the IDs after running tests, to ensure that the following group of tests can run without tossing primary key breach within the database. Basically can cleanup the database after each test run, then the issue is completely resolved, else I will need to consider other solutions like producing random IDs and taking advantage of them wherever IDs are needed.

Edit: I must stress that i'm testing something, that is conntacting database, I do not have direct accessibility database. Consider, the services are ours, I'm able to customize the plan to provide any cleanup method if needed.

Unless of course you as testing specific database actions (verifying you are able to query or update the database for instance) your JUnits should not be conntacting a genuine database. Rather you need to mock the database classes. By doing this you do not really need to connect and customize the database and because of this no cleanup is required.

You are able to mock your classes a few various ways. Use a library for example JMock which is going to do all of the execution and validation meet your needs. The most popular method of doing this really is with Dependency Injection. By doing this I'm able to create mock classes that implement my repository connects (you're using connects for the data access layer right? -)) and that i implement just the needed techniques with known actions/return values.

//Example repository interface.
public interface StudentRepository
{
   public List<Student> getAllStudents();
}

//Example mock database class.
public class MockStudentRepository implements StudentRepository
{
   //This method creates fake but known data.
   public List<Student> getAllStudents()
   {
      List<Student> studentList =  new ArrayList<Student>();
      studentList.add(new Student(...));
      studentList.add(new Student(...));
      studentList.add(new Student(...));

      return studentList;
   }
}

//Example method to test.
public int computeAverageAge(StudentRepository aRepository)
{
   List<Student> students = aRepository.GetAllStudents();
   int totalAge = 0;
   for(Student student : students)
   {
      totalAge += student.getAge();
   }

   return totalAge/students.size();
}

//Example test method.
public void testComputeAverageAge()
{
   int expectedAverage = 25; //What the expected answer of your result set is
   int actualAverage = computeAverageAge(new MockStudentRepository());

   AssertEquals(expectedAverage, actualAverage);
}

Spring's unit testing framework has extensive abilities for coping with JDBC. The overall approach would be that the unit tests runs inside a transaction, and (outdoors of the test) the transaction is folded back when the test is done.

It has the benefit of having the ability to make use of your database and it is schema, but without coming to a direct changes towards the data. Obviously, should you really execute a commit within your test, then all bets are off!

For additional reading through, take a look at Spring's documentation on integration testing with JDBC.

Presuming you can get the database: An alternative choice is to produce a backup from the database right before the tests and restore from that backup following the tests. This is often automated.

When writing JUnit tests, you are able to override two specific techniques: setUp() and tearDown(). In setUp(), you are able to set everything thats necessary to be able to test out your code so you do not have to create some misconception in every specific test situation. tearDown() is known as in the end the exam cases run.

If at all possible, you can arrange it so that you can open your database within the setUp() method after which get it obvious everything in the tests and close it within the tearDown() method. This is the way we've done all testing when there exists a database.

Heres a good example:

@Override
protected void setUp() throws Exception {
    super.setUp();
    db = new WolfToursDbAdapter(mContext);
    db.open();

    //Set up other required state and data
}

@Override
protected void tearDown() throws Exception {
    super.tearDown();
    db.dropTables();
    db.close();
    db = null;
}

//Methods to run all the tests