最近在写一模块的代码,不能再像原来一样不认真考虑测试了,因为看看Junit如何写单元测试,这里作一些笔记。
1. 下载junit的包,并在工程中引用。不过还可以在eclipse工程中选择Junit的库。
2. 关于使用Junit的目的。主要有四种:
- 测试某个类的所有方法
- 测试某个类的个别方法
- 测试几个类中的全部方法
- 测试几个类中的指定方法
摘自某参考资料。对此,我觉得我是想测试某个类中的某几个方法,因为觉得这几个方法比较容易出问题,不过这样是显得有些不严谨了。其实往往测关键方法的时候,其中也都涉及到了对其它方法的调用,也可以算作是测试了该类的所有方法。
3. 一个Junit测试类
新建了一个test的包,以后测试的类都可以放在该包下面。
package test;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
public class AppTest extends TestCase {
public void setUp() {
//在其中可以做一些初始化的方法
}
/**
* Create the test case
*
* @param testName
* name of the test case
*/
public AppTest(String testName) {
super(testName);
}
/**
* @return the suite of tests being tested
*/
public static Test suite() {
return new TestSuite(AppTest.class);
}
//具体写测试用例了,名字加函数名,比较清晰
public void testRegexStream() {
// asssertEquals(new Hello().hello(), "Hello");
// assertTrue(xxx.());
}
}