- Install Junit and Eclemma
1.Junit and Hamcrest
First download the junit-4.12.jar and hamcrest-all-1.3.jar and so on from https://github.com/junit-team/junit4/wiki/Download-and-Install.
Then add them to Java Build Path of your project:click project->properties->Java Build Path->Liraries->Add External JARs->choose your jars.
2.Eclemma
Download the eclemma.zip and unpack it.
In Eclipse, click Help->Install New Software->add...->choose your eclemma folder->OK->choose eclemma->finish->Reboot the eclipse.
After installing it, you can see a new button in your eclipse like this:
- Using Junit and Eclemma to test Triangle program
1.Create Triangle.java
package st.lab1;
public class Triangle {
private int a,b,c;
public Triangle(int a, int b, int c)
{
this.a = a;
this.b = b;
this.c = c;
}
public String triangle()
{
if(a + b <= c || b + c <= a || a + c <= b || a <= 0 || b <= 0 || c <= 0)
return "not a triangle";
if((a == b)&&(b == c))
return "equilateral";
else if((a != b)&&(b != c)&&(a != c))
return "scalene";
else
return "isosceles";
}
}
2.Create Junit test case class:
package st.lab1; import static org.junit.Assert.*; 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 java.util.Arrays; import java.util.Collection; @RunWith(Parameterized.class) public class TriangleTest { private int a; private int b; private int c; private String result; private static int count = 1; private Triangle tri; public TriangleTest(int a, int b, int c, String result) { this.a = a; this.b = b; this.c = c; this.result = result; } @Parameters public static Collection data() { return Arrays.asList(new Object[][]{ {1, 1, 2, "not a triangle"}, {1, 1, 1, "equilateral"}, {2, 2, 3, "isosceles"}, {3, 4, 5, "scalene"} }); } @Before public void setUp() throws Exception { System.out.println("Create a triangle " + count); tri = new Triangle(a, b, c); } @Test public void test() { System.out.println("Processing test" + count); assertEquals(result, tri.triangle()); } @After public void tearDown() throws Exception { count++; System.out.println("test " + (count - 1) + " have finished!"); } }
Here I used RunWith(Parameterized.class) to do some test at the same time.The result is like this:
- Another method to finish this testing
I also used RunWith(Suite.class) to finish this task.But this is simply for practice using Junit,It may make this test more difficult.
I create three classes corresponding to three kinds of triangle and the Junit test Classes.
Finally,I used a SuiteTest class to test these three class at the same time.
Source Code:
package st.lab1; public class IsoscelesTriangle { private int a,b,c; public IsoscelesTriangle(int a, int b, int c) { this.a = a; this.b = b; this.c = c; } public String triangle() { if(a + b <= c || b + c <= a || a + c <= b || a <= 0 || b <= 0 || c <= 0) return "not a triangle"; if((a == b && b != c)||(a == c && b != c)||(b == c && a != b)) return "isosceles"; else return "not isosceles"; } }
package st.lab1; public class ScaleneTriangle { private int a,b,c; public ScaleneTriangle(int a, int b, int c) { this.a = a; this.b = b; this.c = c; } public String triangle() { if(a + b <= c || b + c <= a || a + c <= b || a <= 0 || b <= 0 || c <= 0) return "not a triangle"; if(a != b && b != c && a != c) return "scalene"; else return "not scalene"; } }
package st.lab1; public class EquilateralTriangle { private int a,b,c; public EquilateralTriangle(int a, int b, int c) { this.a = a; this.b = b; this.c = c; } public String triangle() { if(a + b <= c || b + c <= a || a + c <= b || a <= 0 || b <= 0 || c <= 0) return "not a triangle"; if((a == b)&&(b == c)) return "equilateral"; else return "not equilateral"; } }
package st.lab1; 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; @RunWith(Parameterized.class) public class IsoscelesTriangleTest { private int a; private int b; private int c; private String result; private static int count = 1; private IsoscelesTriangle tri; public IsoscelesTriangleTest(int a, int b, int c, String result) { this.a = a; this.b = b; this.c = c; this.result = result; } @Parameters public static Collection data() { return Arrays.asList(new Object[][]{ {5, 1, 2, "not a triangle"}, {2, 3, 3, "isosceles"}, {2, 2, 2, "not isosceles"}, }); } @Before public void setUp() throws Exception { System.out.println("Create a isosceles triangle " + count); tri = new IsoscelesTriangle(a, b, c); } @Test public void test() { System.out.println("Processing isosceles test" + count); assertEquals(result, tri.triangle()); } @After public void tearDown() throws Exception { count++; System.out.println("isosceles test " + (count - 1) + " have finished!"); } }
package st.lab1; 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; @RunWith(Parameterized.class) public class ScaleneTriangleTest { private int a; private int b; private int c; private String result; private static int count = 1; private ScaleneTriangle tri; public ScaleneTriangleTest(int a, int b, int c, String result) { this.a = a; this.b = b; this.c = c; this.result = result; } @Parameters public static Collection data() { return Arrays.asList(new Object[][]{ {5, 1, 2, "not a triangle"}, {2, 3, 4, "scalene"}, {2, 2, 2, "not scalene"}, }); } @Before public void setUp() throws Exception { System.out.println("Create a scalene triangle " + count); tri = new ScaleneTriangle(a, b, c); } @Test public void test() { System.out.println("Processing scalene test" + count); assertEquals(result, tri.triangle()); } @After public void tearDown() throws Exception { count++; System.out.println("scalene test " + (count - 1) + " have finished!"); } }
package st.lab1; 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; @RunWith(Parameterized.class) public class EquilateralTriangleTest { private int a; private int b; private int c; private String result; private static int count = 1; private EquilateralTriangle tri; public EquilateralTriangleTest(int a, int b, int c, String result) { this.a = a; this.b = b; this.c = c; this.result = result; } @Parameters public static Collection data() { return Arrays.asList(new Object[][]{ {1, 1, 2, "not a triangle"}, {1, 1, 1, "equilateral"}, {2, 2, 3, "not equilateral"}, }); } @Before public void setUp() throws Exception { System.out.println("Create a equalateral triangle " + count); tri = new EquilateralTriangle(a, b, c); } @Test public void test() { System.out.println("Processing equalateral test" + count); assertEquals(result, tri.triangle()); } @After public void tearDown() throws Exception { count++; System.out.println("equalateral test " + (count - 1) + " have finished!"); } }
package st.lab1; import org.junit.runner.RunWith; import org.junit.runners.Suite; @RunWith(Suite.class) @Suite.SuiteClasses({ EquilateralTriangleTest.class, IsoscelesTriangleTest.class, ScaleneTriangleTest.class, }) public class SuiteTest { }
Result:
We can see that all the three classes that hope to be tested have been 100% covered.