Junit4.5學習筆記

 Junit4.5學習筆記

  1. 測試1 CalcualotrTest.java
  2. 測試2 LargestTest.java
  3. 測試3 TestAll.java

 

要測試代碼Calculator.java

package com.mark.junit4;

public class Calculator {
	
	public int add(int a, int b) {
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		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 divide(int a, int b) throws Exception{
		if(0 == b) {
			throw new Exception("除數不能爲0");
		}
		return a / b;
	}
	
}

 

 Junit4測試代碼CalculatorTest.java

package com.mark.junit4;

import junit.framework.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 Calculator cal;
	
	@BeforeClass
	public static void globalInit() {
		System.out.println("globalInit invoked!");
	}
	
	@AfterClass
	public static void globalDestroy() {
		System.out.println("globalDestroy invoked!");
	}
	
	
	@Before
	public void init() {
		cal = new Calculator();
		System.out.println("before test");
	}
	
	@After
	public void destroy() {
		System.out.println("destroy");
	}
	
	
	@Test(timeout=800)
	public void testAdd() {
		int result = cal.add(3,5);
		
		Assert.assertEquals(8, result);
	}
	
	@Test
	public void testSubtract() {
		int result = cal.subtract(1, 6);
		Assert.assertEquals(-5, result);
		
	}
	
	@Test
	public void testMultiply() {
		int result = cal.multiply(2, 3);
		Assert.assertEquals(6, result);
	}
	
	@Test(expected=Exception.class)
	public void testDivide() throws Exception{
		cal.divide(1, 0);
	}
	
}

 

要測試代碼Largest.java

package com.mark.junit4;

public class Largest {
	public int getLargest(int[] array) throws Exception {
		if(array == null || array.length == 0) {
			throw new Exception("數組不能爲空");
		}
		int result = array[0];
		for(int i=0; i<array.length; i++) {
			if(result < array[i]) {
				result = array[i];
			}
		}
		
		return result;
	}
}

 

Junit4測試代碼LargestTest.java

package com.mark.junit4;

import junit.framework.Assert;

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

public class LargestTest {

	private Largest largest;
	
	@Before
	public void setUp() throws Exception {
		largest = new Largest();
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testGetLargest() {
		int[] array = { 1, 9, -10, -20, 23, 34 };

		int result = 0;

		try {
			result = largest.getLargest(array);
		} catch (Exception ex) {
			Assert.fail("測試失敗");
		}

		Assert.assertEquals(34, result);
	}
	
	@Test(expected = Exception.class)
	public void testGetLargest2() throws Exception{
		System.out.println("execute!");
		largest.getLargest(null);
	}
	
	@Test(expected = Exception.class)
	@Ignore("no ready yet")
	public void testGetLargest3() throws Exception{
		largest.getLargest(new int[] {});
	}

}

 

Junit測試集合TestAll .java

package com.mark.junit4;

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

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

}

 Junit測試集合TestAll2.java

 

package com.mark.junit4;

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

@RunWith(Suite.class)
@SuiteClasses(TestAll.class)
public class TestAll2 {

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章