原博客:http://blog.csdn.net/jaune161/article/details/40024577
JUnit是由 Erich Gamma 和 Kent Beck 编写的一个回归测试框架(regression testing framework)。Junit测试是程序员测试,即所谓白盒测试,因为程序员知道被测试的软件如何(How)完成功能和完成什么样(What)的功能。Junit是一套框架,继承TestCase类,就可以用Junit进行自动测试了。
JUnit是Java开发中使用最多的测试框架之一。
本系列文章讲的所有内容都是基于Maven3+JUnit4.11,要使用JUnit只需要将JUnit的jar包引入到项目中即可。
首先写要测试的类
下面写测试用例
测试方法只需在方法上加上@Test注释就可以了,assertEquals方法有两个参数第一个是期望值,第二个是实际结果。
运行测试用例之后发现wordFormat4DBNull和wordFormat4DBBegin方法失败,即空参数和首字母大写的没有通过测试,然后修改方法,加上null判断和首字母大写判断,如下
再运行测试用例,这次测试通过,现在的代码已经比较稳定,可以提供给其他模块使用了。如果对此方法进行了修改或优化,只需要运行一次测试用例就可以知道方法是否正常。
在写测试用例时要尽量考虑到业务中可能出现的情况,是测试用例更加完善。