Junit,hamcrest,eclemma Unit Test Basics (JAVA)

Source: Internet
Author: User

This experiment is the basic application of Junit,hamcrest,eclemma unit test, starting from the installation. The language chosen here is java,ide for Eclipse (mars.2)

1 ) Install JUnit, hamcrest and Eclemma

1.1 Installing Juint,hamcrest

Both of these installations I used the same method of importing the jar package, selecting the Java build path in the "Project", "Properties", and importing the jar packages that were prepared externally, I am importing JUnit (4.12) and hamcrest-all-1.3. After importing, you can generate test instances and test them.

1.2 Installing Eclemma

Installing Eclemma I'm using a method that uses Eclipse to add a program. With the site http://update.eclemma.org/added, after the successful addition of the Eclemma icon appears and can be overwritten detection.

2 ) write the program and test

2.1 Writing a program

The procedure below can be used to judge whether a triangle is an equilateral isosceles or a bevel. However, it is not possible to detect whether three edges can form a triangle.

Package Mainapp;

Public class triangle{

Public int a,b,c;//input parameter

Public int result;//Store result

Public int judge (int A,int b,int c) {

if (A = = B&&b ==c) {

result = 1;

}

Else if (A = = b| | b = = C| | C ==a) {

result = 2;

}

Else

{

result = 3;

}

return result;

}

}

2.2 Generating Test Cases

First create a second source folder with the same name package

Then right-click on Triangle.java to create a new junit test case:

Choose to create in the package you just created, and then the program will help us generate a test case named Triangletest in that package, partially modified to get the test case:

Package Mainapp;

Import static org.junit.assert.*;

Import Org.junit.Before;

Import Org.junit.Test;

public class Triangletest {

Triangle Tri;

int result;

@Before

public void SetUp () throws Exception {

Tri = new triangle ();

}

@Test

public void Testequilateral () {

result = Tri.judge (2, 2, 2);

Assertequals (1,result);

}

@Test

public void Testisosceles () {

result = Tri.judge (2, 2, 3);

Assertequals (2,result);

}

@Test

public void Testscalene () {

result = Tri.judge (3, 2, 4);

Assertequals (3,result);

}

}

Then right-click the test case, run it, and run it in the JUnit test case. Get the running result:

There is no overlay test using Eclemma, we click on the Eclemma icon and select Triangletest, then get the overlay result:

The test case is completely overwritten with detailed information appearing below. This unit test is complete.

3 ) Insufficient experimental

The test procedure in this experiment has an error in itself and will not be detected by the test case. At the same time, the test case only uses the Assertequal () function, the function itself as if the junit interior has been integrated, and does not use the Hamcrest other matching device.

Junit,hamcrest,eclemma Unit Test Basics (JAVA)

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.