1。首先写一个java类,此类中有要测试的方法。add()和sub()
AddAndSub.java
package com.zhaosoft.bean;
public class AddAndSub {
public static int add(int m, int n) {
int num = m + n;
return num;
}
public static int sub(int m, int n) {
int num = m - n;
return num;
}
}
2。创建测试类,该类继承于TestCase类。
TestAddAndSub .java
package com.zhaosoft.test;
import junit.framework.TestCase;
import com.zhaosoft.bean.AddAndSub;
public class TestAddAndSub extends TestCase {
private int m=0;
private int n=0;
protected void setUp(){
m=4;
n=6;
}
protected void tearDown(){
m=0;
n=0;
}
//测试方法
public void testadd(){
assertEquals(10,AddAndSub.add(6,4));
}
public void testsub(){
assertEquals(2,AddAndSub.sub(6,4));
}
public static void main(String[] args) {
junit.textui.TestRunner.run(TestAddAndSub.class);
}
}