本篇文章将会介绍Eclipse中Junit与Eclemma插件安装与使用
1.Install Junit and Eclemma
1.1. Install Junit
Download necessary JARs packages
Click right key on the project – Properties – Java Build Path – Libraries – Add External JARs
Add Junit and harmcrest-all-*
1.2. Install Eclemma
- Help –- Install New Software –Add
Input the name eclemma and the Location with http://update.eclemma.org.
Select the eclemma and complete click Next
- Reboot the eclipse to complete the installation
2.Using Junit and eclemma
- Under src folder, create com.Triangle package
- Write Triangle.java
1 | package com.Triangle; |
- Create new source folder test and create com.Triangle package similarly
- Write several classes for test
1 | package com.Triangle; |
In order to practice @Runwith(Suite.class), I also write some other test classes
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
54package com.Triangle;
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.Collection;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import com.Triangle.Triangle;
(Parameterized.class)
public class PositiveTest {
private int a;
private int b;
private int c;
private String expected;
private Triangle triangle;
public PositiveTest(int a, int b, int c, String expected){
this.a = a;
this.b = b;
this.c = c;
this.expected = expected;
}
public void setUp(){
System.out.println("Before Positive Test");
this.triangle = new Triangle(this.a, this.b, this.c);
}
public void tearDown(){
System.out.println("After Positive Test");
}
public static Collection<Object[]> getData(){
return Arrays.asList(new Object[][]{
{3,3,3,"positive"}
});
}
public void testAdd(){
assertEquals(this.expected, triangle.isPositive());
}
}
1 | package com.Triangle; |
1 | package com.Triangle; |
1 | package com.Triangle; |
SuiteTest.java
1 | package com.Triangle; |
Result
Click right key on SuiteTest.java -> Coverage as -> JUnit Test
Except SuiteTest.java itself hasn’t been covered, other classes have been covered already.
If run PositiveTest.java individually, there will be more paths that won’t be covered
Green: All Covered
Red: All Missed
Yellow: Some Branches Missed