1, installation
Download the JUnit and hamacrest jar packages
Select Help->eclipse marketplace-> Search eclemma,install;
2, add JUnit and Hamacrest jar packages after new project
3. Create a test class
Two code folders, create a package with the same name, SRC write a procedure to determine the type of triangle, test write using JUnit code
4, test the code
The Setup method is executed before any method is executed, preceded by a @before
Adding @test before a function means that the method is a unit test
The code being tested is as follows:
Package Softwaretest;public class Triangle {//is equilateral triangle, is return 1, not return 2, cannot form a triangle return 0public int isequ (int a, int b,int c) { int r=0; if ((a+b<=c) | | | (b+c<=a) | | (a+c<=b)) {r=0;} Else{if (a==b&&a==c) {r=1;} else{r=2;}} return r; } Whether for isosceles Triangle, is to return 1, not return 2, cannot form a triangle return 0public int isiso (int a, int b,int c) { int r=0; if ((a+b<=c) | | | (b+c<=a) | | (a+c<=b)) {r=0;} Else{if (a==b| | a==c| | B==C) {r=1;} else{r=2;}} return r; } Whether it is not equilateral triangle, is returning 1, not returning 2, cannot form a triangle return 0public int Issca (int a, int b,int c) {int r=0;if ((a+b<=c) | | (b+c<=a) | | (a+c<=b)) {r=0;} Else{if (a!=b&&a!=c&&b!=c) {r=1;} else{r=2;}} return r; }}
The test code looks like this:
Package Softwaretest;import Org.junit.Before; Import Org.junit.Test; Static import of the import static org.junit.assert.*; public class Testtriangle {private Triangle tri;//executes any method before executing the Setup method @Before public void SetUp () { tri = New Triangle (); } @Test public void testisequ () { assertequals ("Judging equilateral triangle problems", 1, tri.isequ (2, 2, 2)); } @Test public void Testisiso () { assertequals ("Judging equilateral triangle problems", 1, Tri.isiso (2, 2, 3)); } @Test public void Testissca () { assertequals ("Judging equilateral triangle problems", 0, Tri.issca (1, 2, 3));} }
5, test results
Code Coverage Report:
Example of installing and judging triangles using Junit, hamacrest, and Eclemma