JUnit4 我的简明教程

 1.计算机类,这是实现功能代码的主体部分,实现了计算机的简单功能

package com.liutaw.test;

public class Calculator {
 
 private static int result;
 
 public void add(int n) {
  result += n;
 }
 
 public void substract(int n) {
  result -= n;
 }
 
 public void multipy(int n) {
  result *= n;
 }
 
 public void divide(int n) {
  result /= n;
 }
 
 public void square(int n) {
  result = n * n;
 }
 
 public void squareRoot(int n) {
  for(;;);
 }
 
 public void clear() {
  result = 0;
 }
 
 public int getResult() {
  return result;
 }
 
}
2.导入JUnit4的包,新建测试类,我一共建立了3个测试类,分别是:单独的测试、多参数的测试和打包测试

单独的测试:(我对加减乘除进行测试)

package com.liutaw.test;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class CalculatorTest {

 private static Calculator calculator = new Calculator();
 
 //只在测试用例初始化时,执行 @BeforeClass 方法
 //当所有测试执行完毕之后,执行 @AfterClass 进行收尾工作
 //比如说可以用来打开文件或者关闭文件,如果每次读取都操作一次太耗费时间了
 //而且方法必须是public static 的
 @BeforeClass
 public static void setUpBeforeClass() throws Exception {
 }

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

 //Fixture 固定代码段
 //Fixture 的含义是"在某些阶段必然被调用的代码 "
 
 //@Before 在任何一个测试执行之前必须执行的代码(Fixture)
 @Before
 public void setUp() throws Exception {
  calculator.clear();
 }

 //@After 在任何测试执行之后需要进行的收尾工作 (Fixture)
 @After
 public void tearDown() throws Exception {
 }

 //@Test 这是一个测试方法。
 //对于方法的声明也有如下要求:名字可以随便取,没有任何限制,但是返回值必须为 void ,
 //而且不能有任何参数。如果违反这些规定,会在运行时抛出一个异常。
 //至于方法内该写些什么,那就要看你需要测试些什么了。
 @Test
 public void testAdd() {
  calculator.add(2);
  calculator.add(3);
  //断言语句
  //import static org.junit.Assert.*;
  assertEquals(5,calculator.getResult());
 }

 @Test
 public void testSubstract() {
  calculator.add(10);
  calculator.substract(2);
  assertEquals(8,calculator.getResult());
 }

 @Test
 public void testMultipy() {
  calculator.add(3);
  calculator.multipy(2);
  assertEquals(6,calculator.getResult());
 }
 
 @Test(expected = ArithmeticException.class)
 public void testDivideZero() {
  calculator.divide(0);
 }

 @Test
 public void testDivide() {
  calculator.add(8);
  calculator.divide(4);
  assertEquals(2,calculator.getResult());
 }
 //其他标示:
 //@Ignore 某些方法尚未完成,暂不参与此次测试 
 
}

多参数化的测试类(这里是2个参数,一个是输入前的数据,一个是期望的结果,其实还可以扩展一下,扩展到一个以上)

package com.liutaw.test;

import static org.junit.Assert.*;

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

import junit.framework.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;

//首先要加入标示@RunWith(Parameterized.class)说明 这个测试类是参数化单元测试
@RunWith(Parameterized.class)
public class CalculatorSquareTest {

 //新增一个测试实例
 private static Calculator c = new Calculator();
 //添加参数化查询用到的全局变量
 private int param;//存放参数,如果不止一个参数,那就再定义一个全局变量
 private int result;//存放结果
 
 //构造函数,其值来自于参数化查询的
 public CalculatorSquareTest(int param, int result) {
  this.param = param;
  this.result = result;
 }
 
 //新增一个参数化查询的元数据(定义一测试数据的集合)
 //顺序是 { 参数,期待的结果 }
 @Parameters
 public static Collection data() {
  return Arrays.asList(new Object[][]{
    {2,4},//全局变量是2个,这里就是2个,如果是2个,也要再输入一个参数
    {0,0},
    {-3,9},
    {1,1},
    {100,10000}
  });
 }
 
 @Before
 public void setUp() throws Exception {
  c.clear();
 }

 @Test
 public void testSquare() {
  c.square(param);
  assertEquals(result, c.getResult());
 }

}

打包测试类

package com.liutaw.test;

import static org.junit.Assert.*;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({
 
 //其它代码都一样的,只要修改这里就好了
 //打包测试的功能,将所有需要运行的测试类集中起来,一次性的运行完毕,方便测试
 CalculatorSquareTest.class,
 CalculatorTest.class
})
//以上已经实现了打包测试类的全部功能,
//下面的类不需要添加任何功能,只需要起一个好的名字就行了
public class CalculatorPackageTest {
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值