用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

使用Junit做单元测试

2014-12-29 作者: java源代码大全举报

[java]代码库

package andycpp;
public class Calculator {
    private static int result; // 静态变量,用于存储运行结果
    public void add(int n) {
        result = result + n;
    }
    public void substract(int n) {
        result = result - 1;  //Bug: 正确的应该是 result =result-n
    }
    public void multiply(int n) {
    }         // 此方法尚未写好
    public void divide(int n) {
        result = result / n;
    }
    public void square(int n) {
        result = n * n;
    }
    public void squareRoot(int n) {
        for (;;) ;            //Bug : 死循环
    }
    public void clear() {     // 将结果清零
        result = 0;
    }
    public int getResult() {
        return result;
    }
}



package andycpp;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
public class CalculatorTest {
    private static Calculator calculator = new Calculator();
    @Before
    public void setUp() throws Exception {
        calculator.clear();
    }
    @Test
    public void testAdd() {
        calculator.add(2);
        calculator.add(3);
        assertEquals(5, calculator.getResult());
    }
    @Test
    public void testSubstract() {
        calculator.add(10);
        calculator.substract(2);
        assertEquals(8, calculator.getResult());
    }
    @Ignore("Multiply() Not yet implemented")
    @Test
    public void testMultiply() {
    }
    @Test
    public void testDivide() {
    	calculator.add(8);
        calculator.divide(2);
        assertEquals(4, calculator.getResult());
    }
    @Test(expected  =  ArithmeticException. class)
    public void testDivide1(){
    	calculator.add(8);
    	calculator.divide(0);
    }
    @Test(timeout=1000)
    public void testSquareRoot(){
    	calculator.squareRoot(2);
    	assertEquals(4, calculator.getResult());
    }
}


package andycpp;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import java.util.Arrays;
import java.util.Collection;
@RunWith(Parameterized.class)
public class CalculatorSquareTest {
	private static Calculator calculator = new Calculator();
	private int param;
	private int result;
	@Parameters
	public static Collection data()
	{
		return Arrays.asList(new Object[][]{
			{2,4},{0,0},{-3,9}
				})
				;
	}
	public CalculatorSquareTest(int p, int r)
	{
		this.param = p;
		this.result = r;
	}
	@Before
	public void setUp() throws Exception {
		calculator.clear();
	}
	@Test
	public void testSquare() {
		calculator.square(param);
		assertEquals(result, calculator.getResult());
	}
}


package andycpp;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses
(
	{
		CalculatorTest.class,
		CalculatorSquareTest.class
	}
		)
public class TestSuite {
}



网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...