Junit单元测试2

  Junit单元测试2

一、实验目的

  1. 学习使用 JUnit4.X 进行单元测试;
  2. 应用 JUnit4.X 进行单元测试,为工程项目中的类,设计测试类,并且运用白盒测试和黑盒测试方法为类中的方法设计足够充分的测试用例集,从而保证每一个类代码的正确性和健壮性。

二、实验内容

题目1:使用参数化运行器(Parameterized.class)对税收类tax进行测试,建立测试类TestTax;

步骤一:创建税收类

步骤二:建立测试类TestTax;

@RunWith(Parameterized.class)

public class TaxTest extends Tax {

    private Tax tax;

    private float finput;//工资

    private float expectedTax;//期望税收

    @Parameterized.Parameters

    public static List<Object[]> data(){

        Object[][] object = {{0f,0f},{4500f,0f},{4999f,0.0f},{5000f,0.0f},{6500.0f,44.0f},           {8000f,239.0f},{8500.0f,389.0f},{16000f,1814.0f},{17000.0f,2014.0f},{17800f,2174.0f},       {29000.0f,4614.0f},{30000f,4864.0f},{35000f,6114.0f},{39000.0f,7114.0f},{40000f,7364.0f},  {41000f,7614.0f},{59000.0f,12814.0f},{60000.0f,13114.0f},{61000.0f,13414.0f},{84000.0f,21264.0f},{85000.0f,21614.0f},{86000.0f,22014.0f}};

        return Arrays.asList(object);

}

    @Before

    public void setUp() throws Exception {

        tax = new Tax();

    }

    public TaxTest(float input, float expectedTax){

        this.finput = input;

        this.expectedTax = expectedTax;

    }

    @After

    public void tearDown() throws Exception {

    }

    @Test

    public void testCountTax() {

       assertEquals(this.expectedTax, tax.countTax(this.finput),0.0);}}

步骤三:根据税收表格设计测试用例并进行测试

测试用例

月薪

全月应缴纳所得税

实际输出

测试结果

1

0

0

0

2

4500

0

0

3

4999

0

0

4

5000

0

0

5

6500

44

44

6

8000

239

239

7

8500

389

389

8

16000

1814

1814

9

17000

2014

2014

10

17800

2174

2174

11

29000

4614

4614

12

30000

4864

4864

13

35000

6114

6114

14

39000

7114

7114

15

40000

7364

7364

16

41000

7614

7614

17

59000

12814

12814

18

60000

13114

13114

19

61000

13414

13414

20

84000

21264

21264

21

85000

21614

21614

22

86000

22014

22014

 

题目2:设计一个测试类 TestLargest 对最大数类 Largest 进行单元测试;

步骤一:设计测试类 TestLargest

public class LargestTest extends Largest {

    Largest largest = new Largest();

    int[] array;

    @Before

    public void setUp() throws Exception {

    }

    @After

    public void tearDown() throws Exception {

    }

     @Test(expected = Exception.class)

    public void testGetLargest() throws Exception{

        largest.getLargest(array);

    }

     @Test(expected = Exception.class)

    public void testGetLargest1() throws Exception{

        array = new int[]{};

        largest.getLargest(array);

    }

    @Test

    public void testGetLargest2() throws Exception{

        array = new int[]{2,6,6,9};

        assertEquals(9,largest.getLargest(array));

}}

步骤二:进行junit单元测试

 

题目3:在实验六的基础上,测试类 TestAll,对已经建立的测试类进行套件化的测试。

步骤一:设计测试类TestAll

@RunWith(Suite.class)

@SuiteClasses({ LargestTest.class, TaxTest.class })

public class TestAll {

}

步骤二:进行测试

 

三、实验分析

题目一根据税收表来对税收类tax进行测试,实现时有点繁琐且容易混乱。在设计测试用例之后需要根据具体公式对税收进行计算,较为复杂。且测试用例的选择需要比较容易算的整数,不然容易报错。题目二是对Largest类进行测试,我做的测试较为简单。题目三只需要编写一个简单的TestAll测试类即可。

实验思考题:

  1. JUnit 的开发者是谁?

答:JUnit是一个开源的java单元测试框架。在1997年,由 Erich Gamma 和 Kent Beck 开发完成。

四、实验总结

   通过这次实验,又对单元测试有了更深刻的了解,知道了单元测试也可以设计测设用例来进行测试。在测试的过程中,由于我测试用例的选择数字不宜计算,导致程序显示Failure,且修改之后由于小数点后位数太多一直报错,经过修改测试成功。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

如果说爱他

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值