Junit测试后的数据库清理 [英] Database cleanup after Junit tests

查看:114
本文介绍了Junit测试后的数据库清理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用Junit测试一些Thrift服务。当我将测试作为Thrift客户端运行时,服务会修改服务器数据库。我无法找到一个好的解决方案,可以在每次测试运行后清理数据库。
清理很重要,特别是因为ID必须是唯一的,目前从XML文件中读取。现在,我必须在运行测试后手动更改ID,以便下一组测试可以运行而不会在数据库中抛出主键冲突。如果我可以在每次测试运行后清理数据库,那么问题就完全解决了,否则我将不得不考虑其他解决方案,比如生成随机ID并在需要ID的地方使用它们。

I have to test some Thrift services using Junit. When I run my tests as a Thrift client, the services modify the server database. I am unable to find a good solution which can clean up the database after each test is run. Cleanup is important especially because the IDs need to be unique which are currently read form an XML file. Now, I have to manually change the IDs after running tests, so that the next set of tests can run without throwing primary key violation in the database. If I can cleanup the database after each test run, then the problem is completely resolved, else I will have to think about other solutions like generating random IDs and using them wherever IDs are required.

编辑:我想强调一下,我正在测试一个写入数据库的服务,我没有直接访问数据库。但是,由于服务是我们的,我可以修改服务,以便在需要时提供任何清理方法。

I would like to emphasize that I am testing a service, which is writing to database, I don't have direct access to the database. But since, the service is ours, I can modify the service to provide any cleanup method if required.

推荐答案

除非您测试特定的数据库操作(例如,验证您可以查询或更新数据库),否则您的JUnits不应该是写入真实的数据库。相反,你应该模拟数据库类。这样你实际上不必连接和修改数据库,因此不需要清理。

Unless you as testing specific database actions (verifying you can query or update the database for example) your JUnits shouldn't be writing to a real database. Instead you should mock the database classes. This way you don't actually have to connect and modify the database and therefor no cleanup is needed.

你可以用几种不同的方式模拟你的类。您可以使用 JMock 等库,它将为您完成所有执行和验证工作。我个人最喜欢的方法是使用依赖注入。这样我就可以创建实现我的存储库接口的模拟类(你正在使用数据访问层的接口吗?;-))并且我只使用已知的操作/返回值来实现所需的方法。

You can mock your classes a couple of different ways. You can use a library such as JMock which will do all the execution and validation work for you. My personal favorite way to do this is with Dependency Injection. This way I can create mock classes that implement my repository interfaces (you are using interfaces for your data access layer right? ;-)) and I implement only the needed methods 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);
}

这篇关于Junit测试后的数据库清理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆