当想要对方法进行单独的测试的时候不妨试试使用Junit
使用方法
/**
*
* @author zero
* 1. ProjectName-->build path-->configure build path-->add library-->JUnit-->choose version
在项目名称上右键按照上述的目录结构添加JUnit工具包
* 2. add annotation @Test on your method that needs to be test Ctrl+Shift+O import relevant class
在需要单元测试的方法上添加@Test注解 并使用快捷键Ctrl+Shift+O引入相关的包文件
* 3. click the right key of your mouse and choose run as Junit test
*保存后 单击鼠标右键选择用JUnit运行程序,这样一来,被Test注解的方法就会被执行
* besides of that, there are two annotations can be used to test @Before and @After
另外,在Junit版本4中还有@Before 和 @After 两种新的注解,分别在@Test注解的方法前后执行
* executing steps : @Before @Test @After
* tips: @Before and @After only can be used in JUnit version 4
*/
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
*
* @author zero
* 1. ProjectName-->build path-->configure build path-->add library-->JUnit-->choose version
* 2. add annotation @Test on your method that needs to be test Ctrl+Shift+O import relevant class
* 3. click the right key of your mouse and choose run as Junit test
*
* besides of that, there are two annotations can be used to test @Before and @After
* executing steps : @Before @Test @After
* tips: @Before and @After only can be used in JUnit version 4
*/
public class Junit {
@Before
public void beforeTest() {
System.out.println("This method is called before Test method");
}
@After
public void afterTest() {
System.out.println("This method is called after Test method");
}
@Test
public void unitTest() {
System.out.println("run the method lonely");
}
public static void main(String[] args) {
}
}
执行结果为:
This method is called before Test method
run the method lonely
This method is called after Test method
以上就是单元测试的基本使用方法(这是在Eclipse环境的测试)