实践二

一、题目简介
求两个整型数据x,y的乘除运算,并利用junit进行测试。
二、源码的github链接
https://github.com/zhuwenxue/math

三、所设计的模块测试用例、测试结果截图

这里编写的第一个代码为:

Math.java

public class Math {
public static int divide(int x,int y) {
return x/y;
}

public static int multiple(int x,int y) {
return x*y;
}
}

 

编写的测试代码:

import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.Collection;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Ignore;
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 MathTest {
int faciend;
int multiplicator;
int result;

public MathTest(int faciend, int multiplicator, int result) {
this.faciend = faciend;
this.multiplicator = multiplicator;
this.result = result;
}

@BeforeClass
public static void setUpBeforeClass() throws Exception {
}

@AfterClass
public static void tearDownAfterClass() throws Exception {
}

@Test(expected=ArithmeticException.class)
public void testDivide() {
assertEquals(3,Math.divide(9,3));
assertEquals(3,Math.divide(10,3));
Math.divide(10,0);//除数不能为0,会抛出异常

}

//@Ignore("忽略乘法测试")
@Test
public void testMultiple() {
assertEquals(result,Math.multiple(faciend,multiplicator));
}

@Parameters
public static Collection multipleValues() {
return Arrays.asList(new Object[][] {
{3, 2, 6 },
{4, 3, 12 },
{21, 5, 105 },
{11, 22, 242 },
{8, 9, 72 }});
}

}

进行运行时截图:

对编写的测试代码进行运行:

四、问题及解决方案、心得体会

通过这次的实践学习,对github源代码的喜爱更加的加深了。这与他方便实用的特点是分不开的,而关于Junit4进行程序模块的测试,回归测试了解到了它的特点。

Junit简介:它包括了以下的特性:

  1 对预期结果作断言
  2 提供测试装备的生成与销毁
  3 易于组织执行测试
  4 图形与文字界面的测试器

这次的实验收获很多,正确理解并能够熟练操作和使用Java类。在学习Java的同时学会利用Junit4进行程序模块的测试,回归测试,并熟练使用Github平台进行代码管理。

posted @ 2015-04-28 13:37  氷菓Bingo  阅读(125)  评论(4编辑  收藏  举报