ST Lab1 junit test

时间:2023-03-08 18:16:57
ST Lab1 junit test

代码地址:  https://github.com/newff/st-lab1

Tasks:

  1. Install Junit(4.12), Hamcrest(1.3) with Eclipse
  2. Install Eclemma with Eclipse
  3. Write a java program for the triangle problem and test the program with Junit.

a)       Description of triangle problem:

Function triangle takes three integers a,b,c which are length of triangle sides; calculates whether the triangle is equilateral(等边), isosceles(等腰), or scalene(斜角体). 

1. Install Junit(4.12), Hamcrest(1.3) with Eclipse

  To install jar package into Eclipse, we can use Maven or download the jar package the add to Eclipse. In my earlier blogs I have telled how to add jar package through Maven, in this passage I tell how to download and add directly.

  Online websites:

    Junit:         http://junit.org/junit4/

    hamcrest:  http://search.maven.org/#search|ga|1|g%3Aorg.hamcrest

ST Lab1 junit test

ST Lab1 junit test

  hamcrest-all.* is recommended.

  Choose the proper version and download it.

Open Eclipse, file->new->Java Project, name it "javaPro", it finished to new a new project. Then, on your "javaPro", right click->properties->Java Build Path->Libraries->Add External JARs, select the directory of your jar packages. click Apply and OK. You can see the two packages in your Referenced Library.

ST Lab1 junit test

ST Lab1 junit test

2. Install Eclemma with Eclipse

website:      http://www.eclemma.org/installation.html#marketplace

There are three options for us to install the Eclemma on this website, you can choose anyone of them. I chose the first one and then restart my eclipse, it was already installed.

In your "javaPro", help->Eclipse MarketPlace, search "Eclemma ",and then install. It finished to install the Eclemma.

ST Lab1 junit test

3.Write a java program for the triangle problem and test the program with Junit. 

The java program is on the github. To test the program with junit, we have to right click the "Triangle.java", and then new->Junit Test Case,

Browse the functions that we  want to test, then finish.

ST Lab1 junit testST Lab1 junit test

Write the test function for the Triangle function, then, add some test cases, click the button to run and we will see the result.

ST Lab1 junit test

4.Result of the test

When I test all of the four conditions, the coverage is 100%.

ST Lab1 junit test

ST Lab1 junit test

One error I have made in my lab is that when I judge whether it is a riangle, I wrote this"if((a+b>c && a-b<c) || (a+c>b && a-c<b) || (b+c>a && b-c<a))",this program get the wrong answer. Then I fixed the error and the program become right.

ST Lab1 junit test

 5.Prolongation

What's more, I tried to use Parameters in my junit test.

These are my codes:

 package javaPro;

 import static org.junit.Assert.*;

 import java.util.Arrays;
import java.util.Collection; 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 TriangleTest {
private int a;
private int b;
private int c;
private int expected;
private Triangle triangle; public TriangleTest(int a, int b, int c, int expected){
this.a = a;
this.b = b;
this.c = c;
this.expected = expected;
}
@Before
public void setUp() throws Exception {
System.out.println("Before test");
triangle = new Triangle();
} @Parameters
public static Collection<Object[]>getData(){
return Arrays.asList(new Object[][]{
{1, 2, 3, 3},
{1, 1, 1, 0},
{2, 2, 3, 1},
{3, 4, 5, 2}
});
}
@Test
public void testTriangles() {
assertEquals (this.expected, triangle.triangles(a, b, c));
// assertEquals (0, triangle.triangles (1,1,1));
// assertEquals (1, triangle.triangles (2,2,3));
// assertEquals (2, triangle.triangles (3,4,5));
} }

And this is the result, I have also put my code on the Github.

ST Lab1 junit test

I made a mistake that the name of the constructed function must be the same with the name of the class. It worked after I have fixed this mistake.

ST Lab1 junit test