使用junit测试java程序方法
编程工具/环境 Java junit
参考资料
下载安装
使用方法(以HelloWorld为例子)
先写好要测试的类和用以测试的类,如下:
// HelloWorld.java
importjava.util.*;
publicfinalclassHelloWorld{
privateStringstr;
publicstaticvoidmain(String[]args){
HelloWorldhw=newHelloWorld();
hw.hello();
System.out.println(hw.str);
}
publicvoidhello(){
str="Hello World!";
}
publicStringgetStr(){
returnstr;
}
}
在junit4.x版本中,我们可以用@test来表示一个测试。
// HelloWorldTes.java
importstaticorg.junit.Assert.*;
importorg.junit.Test;
publicclassHelloWorldTest{
publicHelloWorldhelloworld=newHelloWorld();
@Test
publicvoidtestHello(){
helloworld.hello();
ass