JUnit 初体验

JUnit是为Java程序开发者实现单元测试提供的一种框架。JUnit的一大主要特点是,它在执行的时候,各个方法之间是相互独立的,一个方法的失败不会导致别的方法失败,方法之间也不存在相互依赖的关系,彼此是相互独立的。

利用JUnit测试计算器程序

测试Calculator类所有方法(仅有整数计算)

Calculator.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class Calculator {
private static int result; //静态变量,用于存储运行结果
public void add(int n){
result=result+n;
}
public void substract(int n){
result=result-n;
}
public void multiply(int n){
result=result*n;
}
public void divide(int n){
result=result/n;
}
public void square(int n){
result=n*n;
}
public void squareRoot(int n){
for(int i=1; i<=n/2; i++){
if(n==i*i){
result=i;
break;
}
}
}
public void clear(){
result=0; //将结果清零
}
public int getResult(){
return result;
}
}

CalculatorTest.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.internal.runners.TestClassRunner;
import org.junit.runner.RunWith;

@RunWith(TestClassRunner.class)
public class CalculatorTest {

private static Calculator calculator=new Calculator();

@Before //每一个测试之前执行
public void setUp() throws Exception {
calculator.clear();
}

@After //每一个测试之后执行
public void tearDown() throws Exception {
}

@Test(timeout=2000) //设置超时
public void testAdd() {
calculator.add(12);
calculator.add(13);
assertEquals(25, calculator.getResult());
}

@Test(timeout=2000)
public void testSubstract() {
calculator.add(30);
calculator.substract(12);
assertEquals(18,calculator.getResult());
}

@Test(timeout=2000)
public void testMultiply() {
calculator.add(3);
calculator.multiply(8);
assertEquals(24,calculator.getResult());
}

@Test(timeout=2000)
public void testDivide() {
calculator.add(16);
calculator.divide(2);
assertEquals(8,calculator.getResult());
}

@Test(timeout=2000)
public void testSquare() {
calculator.square(8);
assertEquals(64,calculator.getResult());
}

@Test(timeout=2000)
public void testSquareRoot() {
calculator.squareRoot(64);
assertEquals(8,calculator.getResult());
}

}

利用JUnit测试Point2D

Point2D(用于计算两点之间的距离)

Point2D.java

1
2
3
4
5
6
7
8
9
10
11
public class Point2D {
double distance;

void distanceFrom(double a1, double b1, double a2, double b2){
distance = Math.sqrt(Math.pow(a1-a2, 2)+Math.pow(b1-b2, 2));
}

void distanceFromOrigin(double a, double b){
distance = Math.sqrt(Math.pow(a, 2)+Math.pow(b, 2));
}
}

Point2D.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import static org.junit.Assert.*;

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

public class Point2DTest {

Point2D x = new Point2D();

@Before
public void setUp() throws Exception {
}

@After
public void tearDown() throws Exception {
}

@Test(timeout=2000)
public void testDistanceFrom() {
x.distanceFrom(6, 8, 0, 0);
assertEquals(10, x.distance, 0.1);
}

@Test(timeout=2000)
public void testDistanceFromOrigin() {
x.distanceFromOrigin(6, 8);
assertEquals(10, x.distance, 0.1);
}

}
文章目录
  1. 1. 利用JUnit测试计算器程序
    1. 1.1. Calculator.java
    2. 1.2. CalculatorTest.java
  2. 2. 利用JUnit测试Point2D
    1. 2.1. Point2D.java
    2. 2.2. Point2D.java

20160418-junit/

本页二维码