第二次实验报告

一、题目简介

    求两个整型变量x,y之间的乘除运算

 二、源码的github链接

     https://github.com/Accredit/test2

三,代码:

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 }});

         }

 

}

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

首先使用junit进行测试

结果是显示绿色,表示测试通过

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

通过这一次的实验,学会了使用junit,对JAVA的使用有了温习的作用,对JAVA有了更好的理解,学到了很多实用的东西,收获很多。对于junit的学习,我有这些感受, junit使用很方便。在Eclipse的支持下,上手很快,很轻松就可以写出测试用例。写完junit后,我们对我们要写的程序的目的就非常的清晰,写起来相当的快。后面的测试就是自动化的了,不需要我们手工对程序反复检查,也节约的很多时间,并且保证了品质,以及实现结构很精巧。现在仅仅是摸到了JUnit的皮毛,要在大规模开发中使用JUnit,还是有很多问题。  

posted @ 2015-04-27 22:37  颉凌杰  阅读(208)  评论(4编辑  收藏  举报