Eclipse中Junit与Eclemma插件安装与使用

本篇文章将会介绍Eclipse中Junit与Eclemma插件安装与使用

完整代码

1.Install Junit and Eclemma

1.1. Install Junit

  • Download necessary JARs packages

    imgimg

  • Click right key on the project – Properties – Java Build Path – Libraries – Add External JARs

  • Add Junit and harmcrest-all-*

    img

1.2. Install Eclemma

  • Help –- Install New Software –Add
  • Input the name eclemma and the Location with http://update.eclemma.org.

    img

  • 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
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package com.Triangle;

public class Triangle {

int a, b, c;

public Triangle(int x, int y, int z){
a = x;
b = y;
c = z;
}

public String isPositive(){
String msg = "";
if(a > 0 && b > 0 && c > 0){
msg = "positive";
}else{
if(a <= 0)
msg += "a is negative";
if(b <= 0)
msg += "b is negative";
if(c <= 0)
msg += "c is negative";
}
//System.out.println(msg);
return msg;
}

public String isTriangle(){
String msg = "";
if(a+b > c && a+c > b && b+c > a){
msg = "triangle";
}else{
msg = "not a triangle";
}
//System.out.println(msg);
return msg;
}

public String isIsosceles(){
String msg = "";
if(a == b || b == c || c == a){
msg = "isosceles";
}else{
msg = "not isosceles";
}
//System.out.println(msg);
return msg;
}

public String isEquilateral(){
String msg = "";
if(a == b & b == c){
msg = "equilateral";
}else{
msg = "not equilateral";
}
//System.out.println(msg);
return msg;
}

public String judge(){
String msg = "";
msg = this.isPositive();
if(msg.equals("positive")){
msg = this.isTriangle();
if(msg.equals("triangle")){
msg = this.isIsosceles();
if(msg.equals("isosceles")){
msg = this.isEquilateral();
if(!msg.equals("equilateral")){
return "isosceles";
}
}else{
return "triangle";
}
}
}
return msg;
}
}
  • Create new source folder test and create com.Triangle package similarly
  • Write several classes for test
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
package 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;

@RunWith(Parameterized.class)
public class JudgeTest {
private int a;
private int b;
private int c;
private String expected;
private Triangle triangle;

public JudgeTest(int a, int b, int c, String expected){
this.a = a;
this.b = b;
this.c = c;
this.expected = expected;
}

@Before
public void setUp(){
System.out.println("Before Judge Test");
this.triangle = new Triangle(this.a, this.b, this.c);
}

@After
public void tearDown(){
System.out.println("After Judge Test");
}


@Parameters
public static Collection<Object[]> getData(){
return Arrays.asList(new Object[][]{
{3,3,3,"equilateral"},
{3,4,3, "isosceles"},
{2,3,4, "triangle"},
{1,2,3, "not a triangle"},
{-1,1,2, "a is negative"},
{1,-2,1, "b is negative"},
{1,1,-2, "c is negative"}
});
}

@Test
public void testAdd(){
assertEquals(this.expected, triangle.judge());
}
}
  • 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
    54
    package 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;

    @RunWith(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;
    }

    @Before
    public void setUp(){
    System.out.println("Before Positive Test");
    this.triangle = new Triangle(this.a, this.b, this.c);
    }

    @After
    public void tearDown(){
    System.out.println("After Positive Test");
    }

    @Parameters
    public static Collection<Object[]> getData(){
    return Arrays.asList(new Object[][]{
    {3,3,3,"positive"}
    });
    }

    @Test
    public void testAdd(){
    assertEquals(this.expected, triangle.isPositive());
    }
    }
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
package com.Triangle;

import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.Collection;

import org.junit.Before;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import com.Triangle.Triangle;

@RunWith(Parameterized.class)
public class TriangleTest {
private int a;
private int b;
private int c;
private String expected;
private Triangle triangle;

public TriangleTest(int a, int b, int c, String expected){
this.a = a;
this.b = b;
this.c = c;
this.expected = expected;
}

@Before
public void setUp(){
System.out.println("Before Triangle Test");
this.triangle = new Triangle(this.a, this.b, this.c);
}

@After
public void tearDown(){
System.out.println("After Triangle Test");
}

@Parameters
public static Collection<Object[]> getData(){
return Arrays.asList(new Object[][]{
{3,3,3,"triangle"}
});
}

@Test
public void testAdd(){
assertEquals(this.expected, triangle.isTriangle());
}
}
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
package 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;

@RunWith(Parameterized.class)
public class IsoscelesTest {
private int a;
private int b;
private int c;
private String expected;
private Triangle triangle;

public IsoscelesTest(int a, int b, int c, String expected){
this.a = a;
this.b = b;
this.c = c;
this.expected = expected;
}

@Before
public void setUp(){
System.out.println("Before Isosceles Test");
this.triangle = new Triangle(this.a, this.b, this.c);
}

@After
public void tearDown(){
System.out.println("After Isosceles Test");
}

@Parameters
public static Collection<Object[]> getData(){
return Arrays.asList(new Object[][]{
{3,3,3,"isosceles"}
});
}

@Test
public void testAdd(){
assertEquals(this.expected, triangle.isIsosceles());
}
}
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
package 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;

@RunWith(Parameterized.class)
public class EquilateralTest {
private int a;
private int b;
private int c;
private String expected;
private Triangle triangle;

public EquilateralTest(int a, int b, int c, String expected){
this.a = a;
this.b = b;
this.c = c;
this.expected = expected;
}

@Before
public void setUp(){
System.out.println("Before Equilateral Test");
this.triangle = new Triangle(this.a, this.b, this.c);
}

@After
public void tearDown(){
System.out.println("After Equilateral Test");
}


@Parameters
public static Collection<Object[]> getData(){
return Arrays.asList(new Object[][]{
{3,3,3,"equilateral"}
});
}

@Test
public void testAdd(){
assertEquals(this.expected, triangle.isEquilateral());
}
}

SuiteTest.java

1
2
3
4
5
6
7
8
9
10
11
package com.Triangle;

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

@RunWith(Suite.class)
@SuiteClasses({ PositiveTest.class, TriangleTest.class , IsoscelesTest.class, EquilateralTest.class, JudgeTest.class})
public class SuiteTest {

}
  • Result

    Click right key on SuiteTest.java -> Coverage as -> JUnit Test

img

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

img

Green: All Covered

Red: All Missed

Yellow: Some Branches Missed