如何写数据访问的单元测试? [英] How to write unit tests for data access?

查看:120
本文介绍了如何写数据访问的单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单元测试在现代软件开发中变得越来越重要,我发现自己迷失在灰尘中。主要是Java程序员,我理解单元测试的基础:具有测试应用程序中基本操作的方法。我可以实现这个简单(通常用作示例)的情况:

  public boolean addNumbers(int a,intb){返回a + b;} 
//上面的单元测试
public boolean testAddNumbers(){return addNumbers(5,10)== 15;}
/ pre>

令我困惑的是如何把它变成实际应用。毕竟,大多数简单的函数已经在API或JDK中。在我的工作中我经常做的一个现实世界的情况是数据访问,即编写DAO以使用数据库。我不能像上面的例子那样编写静态测试,因为从Oracle框中提取一个记录集可以返回一个完整的东西。编写一个广义单元测试,只是在返回集合中寻找一个特定的模式似乎太广泛和无益。相反,我不写单元测试。这是不好的。



另一个例子,我不知道如何处理写测试的情况是web应用程序。我的web应用程序通常建立在J2EE堆栈上,但它们不涉及太多的逻辑。通常,它从数据库传递信息,很少或没有操纵。这些不适合单元测试的目标吗?



简而言之,我发现绝大多数单元测试示例都集中在过于简单的测试用例上,与我的操作无关。我正在寻找任何移动和显示数据,不执行逻辑的应用程序编写单元测试的任何(最好是Java)示例/提示。

解决方案

通常不要为DAO编写单元测试,而是编写集成测试。这些测试基本上包含在




  • 将数据库设置为适合测试的已知状态

  • 调用DAO方法

  • 验证DAO返回正确的数据和/或按预期改变数据库的状态。



无耻的插件: DbSetup 是第一部分的好工具。



要测试应用程序的业务逻辑(复杂与否,不会改变太多),您通常使用嘲笑框架如Mockito:

  SomeDao mockDao = mock(SomeDao.class); 
when(mockDao.findAllEmployees())。thenReturn(Arrays.asList(e1,e2,e3));
SomeService service = new SomeService(mockDao);
someService.increaseSalaryOfAllEmployeees(1000);
// todo验证e1,e2和e3的工资是比之前大1000美元


Unit tests have become increasingly important in modern software development, and I'm finding myself lost in the dust. Primarily a Java programmer, I understand the basics of unit tests: have methods in place that test fundamental operations in your application. I can implement this for the simple (and often used as examples) cases:

public boolean addNumbers(int a, intb) {return a + b;}
//Unit test for above
public boolean testAddNumbers() {return addNumbers(5, 10) == 15;}

What confuses me is how to move this out into practical application. After all, most simple functions are already in APIs or the JDK. A real world situation that I do frequently in my job is data access, i.e. writing DAOs to work with a database. I can't write static tests like the example above, because pulling a record set from an Oracle box can return a whole manner of things. Writing a generalized unit test that just looks for a particular pattern in the return set seems too broad and unhelpful. Instead, I write no unit tests. This is bad.

Another example of a case where I don't know how to approach writing tests is web applications. My web applications are typically built on a J2EE stack, but they don't involve much logic. Generally it's delivering information from databases with little to no manipulation. Are these inappropriate targets for unit tests?

In short, I've found the vast majority of unit test examples to focus on test cases that are too simplistic and not relevant to what I do. I'm looking for any (preferably Java) examples/tips on writing unit tests for applications that move and display data, not perform logic on it.

解决方案

You generally don't write unit tests for DAOs, but integration tests. These tests basically consist in

  • setting the database in a well-known state, suitable for the test
  • call the DAO method
  • verify that the DAO returns the right data and/or changes the stateof the database as expected.

Shameless plug: DbSetup is good tool to do the first part. But other tools exist like DBUnit.

To test the business logic of the app (complex or not, that doesn't change much), you typically mock the DAOs using a mocking framework like Mockito:

SomeDao mockDao = mock(SomeDao.class);
when(mockDao.findAllEmployees()).thenReturn(Arrays.asList(e1, e2, e3));
SomeService service = new SomeService(mockDao);
someService.increaseSalaryOfAllEmployeees(1000);
// todo verify that e1, e2 and e3's salary is 1000 larger than before

这篇关于如何写数据访问的单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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