Unit testing with JUNIT4 in Eclipse (beginner)

Source: Internet
Author: User

when we write large programs, we need to write thousands of methods or functions that may be powerful, but we only use a small subset of the functions of the function in our program, and by debugging we can determine that a small subset of the functionality is correct. However, we should also make sure that each function is completely correct, because if we extend the program in the future and use the other functions of a function, which is a bug , it is definitely a very depressing thing. So, after each function is written, all aspects of the function should be tested, which we call unit tests. (Simple to understand is to test each method)

First, we build a test engineering jutiltes, and then create a method that needs to be tested, the code is as follows.

 Package Util;  Public class MyMath {    publicstaticint sum (int A,int  b) {         return A +b    ;        }  Public Static int divde (int A,int  b) {        return A/b;    }}

The second step is toThe JUnit4 Unit Test package introduces this project: Right-click on the item, point "Properties",

In the pop-up Properties window, first select " Java Build Path, then to the right, select the "Libraries" tab, then on the far right, click " Add Library ... "button, as shown in:

then, in the New Popup dialog box, select JUnit4 and click OK, as shownin theJUnit4 software package is included in our project.


The third step is to generatejunit Test framework: In Eclipse's package Explorer, right-click on the pop-up menu and select "Newà JUnit test case". As shown in the following:

In the popup dialog box, make the appropriate selections as shown in:

In the popup dialog box, click the bottom Next button to jump, as shown in:

After four, the system will automatically generate a new class Mymathtest, which contains some empty test cases. You only need to make these test cases slightly modified to use. The complete mymathtest code is as follows:
 PackageUtil;Import Staticorg.junit.assert.*;Importorg.junit.Test;/** Check the method and click Runtest otherwise the whole method will be run*/ Public classmymathtest {@Test Public voidtestsum () {intActual=mymath.sum (10, 10);//Actual value        intexpected=20;//expectationAssertequals (actual,expected);//Assertion} @Test Public voidTestdivde () {intActual=mymath.divde (10, 10);//Actual value        intexpected=20;//expectationAssertequals (actual,expected);//Assertion    }    }

V. Operation and Knot

Unit testing with JUNIT4 in Eclipse (beginner)

Related Article

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.