package JunitExample;
//执行接口
public interface Calculate {
public int invoke(int... i);
}
加法操作
package JunitExample;
public class AddCalculate implements Calculate{
@Override
public int invoke(int... i) {
// TODO Auto-generated method stub
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int _i=0;
for(int _ni:i)
{
_i+=_ni;
}
return _i;
}
}
除法操作
package JunitExample;
public class DivideCalculate implements Calculate {
@Override
public int invoke(int... i) {
// TODO Auto-generated method stub
int _i = i[0];
for (int m = 1; m < i.length; m++) {
_i /= i[m];
}
return _i;
}
}
乘法操作
package JunitExample;
public class MultiplyCalculate implements Calculate{
@Override
public int invoke(int... i) {
// TODO Auto-generated method stub
int _i=1;
for(int _ni:i)
{
_i*=_ni;
}
return _i;
}
}
接下来是测试代码,在项目下面新建一个test的文件,包名和测试代码所在的包相同
package JunitExample;
import junit.framework.Assert;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
public class CalculateTest2 {
Calculate cal;
int result;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
System.out.println("begin junit");
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
System.out.println("finish junit");
}
/** 测试除法 **/
@Test(expected=ArithmeticException.class,timeout=500)
public void Divide()
{
cal=new DivideCalculate();
result=cal.invoke(3,0);
Assert.fail();
}
@Before
public void setUp() throws Exception {
System.out.println("junit begin every Method");
}
@After
public void tearDown() throws Exception {
System.out.println("junit finish every Method");
}
}
同理测试加法
package JunitExample;
import junit.framework.Assert;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
public class CalculateTest3 {
Calculate cal;
int result;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
System.out.println("begin junit");
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
System.out.println("finish junit");
}
//延迟时间必须小于500毫秒
@Test(timeout=500)
public void testAdd()
{
cal=new AddCalculate();
result=cal.invoke(3,5);
Assert.assertEquals(8, result);
}
@Before
public void setUp() throws Exception {
System.out.println("junit begin every Method");
}
@After
public void tearDown() throws Exception {
System.out.println("junit finish every Method");
}
}
Suite方法的使用:是多个测试文件在一起执行
package JunitExample;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({CalculateTest3.class,CalculateTest2.class})
public class TestAll {
}
使用多个数据测试加法操作
package JunitExample; import java.util.Arrays; import java.util.Collection; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; @RunWith(Parameterized.class) public class FibonacciTest { int result; int[] number; @Parameters @SuppressWarnings("unchecked") public static Collection date() { int[][] arrs={{2,6},{-5,13},{2,-7},{-3,-4}}; return Arrays.asList(new Object[][]{{8,arrs[0]},{8,arrs[1]},{-5,arrs[2]},{-7,arrs[3]}}); } public FibonacciTest(int result,int[] number){ this.result=result; this.number=number; } @Test public void test(){ Calculate cal=new AddCalculate(); Assert.assertEquals(result,cal.invoke(number)); } }