Junit3.8學習筆記
3個測試Demo
- Junit 測試Demo1 Calculator.java
- Junit 測試Demo2 Largest.java
- Junit 自動化測試Demo3 TestAll.java
Junit 測試Demo1
測試代碼Calculator.java
package com.mark.junit;
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 divide(int a, int b) throws Exception{
if(0 == b) {
throw new Exception("除數不能爲0");
}
return a / b;
}
}
測試Junit代碼CalculatorTest.java
package com.mark.junit;
import junit.framework.Assert;
import junit.framework.TestCase;
/**
* 測試類必須要繼承於TestCase父類
* 在junit3.8中,測試方案要滿足如下原則
* 1. public的
* 2. void的
* 3. 無方法參數
* 4. 方法名稱必須以test開頭
*
*/
public class CalculatorTest extends TestCase{
private Calculator cal;
@Override
public void setUp() throws Exception {
cal = new Calculator();
}
@Override
public void tearDown() throws Exception {
}
public void testAdd() {
int result = cal.add(1, 2);
Assert.assertEquals(3, result);
}
public void testSubtract() {
int result = cal.subtract(1, 2);
Assert.assertEquals(-1, result);
}
public void testMultiply() {
int result = cal.multiply(2, 3);
Assert.assertEquals(6, result);
}
public void testDivide() {
int result = 0;
try {
result = cal.divide(6, 2);
} catch (Exception e) {
e.printStackTrace();
Assert.fail();
}
Assert.assertEquals(3, result);
}
public void testDivideDivideByZero() {
Throwable tx = null;
try {
cal.divide(6, 0);
Assert.fail("測試失敗");
} catch (Exception ex) {
tx = ex;
}
Assert.assertEquals(Exception.class, tx.getClass());
Assert.assertEquals("除數不能爲0", tx.getMessage());
}
public static void main(String[] args) {
// junit.textui.TestRunner.run(CalculatorTest.class);
junit.awtui.TestRunner.run(CalculatorTest.class);
}
}
Junit 測試Demo2
測試代碼Largest.java
package com.mark.junit;
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;
}
}
測試junit代碼LargestTest.java
package com.mark.junit;
import junit.framework.Assert;
import junit.framework.TestCase;
public class LargestTest extends TestCase{
private Largest largest;
@Override
protected void setUp() throws Exception {
largest = new Largest();
}
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);
}
public void testGetLargest2() {
Throwable tx = null;
int[] array = {};
try {
largest.getLargest(array);
Assert.fail();
}catch(Exception ex) {
tx = ex;
}
Assert.assertNotNull(tx);
Assert.assertEquals(Exception.class, tx.getClass());
Assert.assertEquals("數組不能爲空", tx.getMessage());
}
public void testGetLargest3() {
Throwable tx = null;
int[] array = null;
try {
largest.getLargest(array);
Assert.fail();
} catch (Exception ex) {
tx = ex;
}
Assert.assertNotNull(tx);
Assert.assertEquals(Exception.class, tx.getClass());
Assert.assertEquals("數組不能爲空", tx.getMessage());
}
}
Junit自動化測試Demo3
Junit自動化測試代碼TestAll.java
package com.mark.junit;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* testSuite
*/
public class TestAll extends TestCase{
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTestSuite(CalculatorTest.class);
suite.addTestSuite(LargestTest.class);
return suite;
}
}