Example of installing and judging triangles using Junit, hamacrest, and Eclemma

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.