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;
}
}