Junit4

Calculator.java

package org.zbq.test;

public class Calculator {
	public int add(int a, int b){
		return a + b;
	}
	
	public int subtract(int a, int b){
		return a - b;
	}
	
	public int multiply(int a, int b){
		return a * b;
	}
	
	public int division(int a, int b) throws Exception {
		if(0 == b){
			throw new Exception("Disior by zero");
		}
		return a / b;
	}
	
}


CalculatorTest

package org.zbq.test;
import static org.junit.Assert.assertEquals;

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

public class CalculatorTest {
	private Calculator cal;
	
	@BeforeClass
	public static void init(){
		System.out.println("Init");
	}
	
	@AfterClass
	public static void destroy(){
		System.out.println("Destroy");
	}
	
	@Before
	public void setUp(){
		System.out.println("Before");
		cal = new Calculator();
	}
	
	@After
	public void tearDown(){
		System.out.println("After");
	}
	
	@Test(timeout = 500)
	public void testAdd(){
		try {
			Thread.sleep(400);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		int result = cal.add(2, 3);
		assertEquals(5, result);
	}
	
	@Test
	public void testSubtract(){
		int result = cal.subtract(5, 3);
		assertEquals(2, result);
	}
	
	@Test
	public void testMultiply(){
		int result = cal.multiply(4, 5);
		assertEquals(20, result);
	}
	
	@Test
	public void testDivision() throws Exception {
		int result = cal.division(10, 3);
		assertEquals(3, result);
	}
	
	@Test(expected = Exception.class)
	public void testDivisionByZero() throws Exception{
		cal.division(5, 0);
	}
	
	@Test
	public void testDivisionByZero2() throws Exception{
		Throwable ex = null;
		
		try{
			cal.division(5, 0);
			Assert.fail("Divisor by zero");
		}catch(Exception e){
			ex = e;
		}
		
		assertEquals(Exception.class, ex.getClass());
		assertEquals("Disior by zero", ex.getMessage());
	}
	
	@Test
	@Ignore("not yet")
	public void ignore(){
		System.out.println("Ignore");
	}
	
}

ParametersTest.java

package org.zbq.test;

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

import static org.junit.Assert.assertEquals;
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)
public class ParametersTest {
	private int result;
	private int a;
	private int b;
	private Calculator cal;
	
	public ParametersTest(int result, int a, int b){
		this.result = result;
		this.a = a;
		this.b = b;
	}
	
	@SuppressWarnings("unchecked")
	@Parameters
	public static Collection parameterData(){
		Object[][] obj = {{5, 2, 3},{-2, 4, -6},{0, 3, -3}};
		return Arrays.asList(obj);
	}
	
	@Before
	public void setUp(){
		this.cal = new Calculator();
	}
	
	@Test
	public void testAdd(){
		assertEquals(this.result, cal.add(this.a, this.b));
	}
	
}

Largest.java

package org.zbq.test;

public class Largest {
	
	public static int getLargest(int[] array) throws Exception{
		if(null == array || array.length ==0){
			new Exception("数组为空");
		}
		int t = array[0];
		for(int i : array){
			if(t < i){
				t = i;
			}
		}
		return t;
	}
}

LargestTest.java

package org.zbq.test;

import static org.junit.Assert.*;

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

public class LargestTest {

	@Before
	public void setUp() throws Exception {
		
	}

	@After
	public void tearDown() throws Exception {
		
	}

	@Test
	public void testGetLargest() throws Exception {
		int[] array = new int[]{1,2,5,3,4};
		int max = Largest.getLargest(array);
		assertEquals(5, max);
	}

	@Test(expected = Exception.class)
	public void testGetLargest2() throws Exception{
		int[] array = new int[]{};
		Largest.getLargest(array);
	}
}

TestAll.java

package org.zbq.test;

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

@RunWith(Suite.class)
@Suite.SuiteClasses({CalculatorTest.class, LargestTest.class})
public class TestAll {
	
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值