A simple example of JUnit doing unit testing

Source: Internet
Author: User

The example is simple, design a program, enter three integers, determine whether the three integers as the length of the three sides can form a triangle, if you can return the type of the triangle (normal triangles, isosceles triangle or equilateral triangle)

Implementation of the code (I write myself casually to do an example, the estimated code quality is poor.) ):

public class Judgetriangle
{
Common triangles
private static int normal_triangle = 0;
Isosceles Triangle
private static int isosceles_triangle = 1;
Equilateral triangle
private static int equilateral_triangel = 2;
Cannot form a triangle
private static int error_code =-1;

public int Getnormaltriangle ()
{
return normal_triangle;
}
public int Getisoscelestriangle ()
{
return isosceles_triangle;
}
public int Getequilateraltriangle ()
{
return Equilateral_triangel;
}
public int GetErrorCode ()
{
return error_code;
}
public int judgetype (int a,int b,int c)
{
if ((a<=0) | | | (b<=0) | | (c<=0))
{
return error_code;
}
if ((A+b<c | | Math.Abs (a) >c) | | (B+c<a | | Math.Abs (b-c) >a) | | (A+c<b | | Math.Abs (A-C) >b))
{
return error_code;
}
if (a==b | | b==c | | c==a)
{
if (a==b && b==c)
{
return Equilateral_triangel;
}
Else
{
return isosceles_triangle;
}
}
Else
{
return normal_triangle;
}
}
}

Unit Test Code:

Import static org.junit.assert.*;
Import Org.junit.After;
Import Org.junit.Before;
Import Org.junit.Test;

public class Judgetriangletest {

private static Judgetriangle Judgetriangle = new Judgetriangle ();

@Before
public void SetUp () throws Exception {
}

@Test
public void Testisnormaltriangle () throws Exception {
Assertequals (Judgetriangle.getnormaltriangle (), Judgetriangle.judgetype (3, 4, 5));
}
@Test
public void Testisisoscelestriangle () throws Exception {
Assertequals (Judgetriangle.getisoscelestriangle (), Judgetriangle.judgetype (2, 2, 3));
}
@Test
public void Testequilateraltriangle () throws Exception {
Assertequals (Judgetriangle.getequilateraltriangle (), Judgetriangle.judgetype (2, 2, 2));
}
@Test
public void Testnottriangle () throws Exception {
Assertequals (Judgetriangle.geterrorcode (), Judgetriangle.judgetype (10,-10, 10));
Assertequals (Judgetriangle.geterrorcode (), Judgetriangle.judgetype (10, 2, 2));
}

@After
public void TearDown () throws Exception {
}

}

These two links are easy to introduce for JUnit understood

Http://tech.sina.com.cn/s/2010-01-18/14081218926.shtml

http://leowzy.iteye.com/blog/793077

A simple example of JUnit doing unit testing

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.