Junit4 unit test

Source: Internet
Author: User

Generally, during the test, the simplest test is to test in the main method. The following code is to test the add () method in Main. Check whether the output result is the same as expected.

If the input parameter add (), the expected output is 8. If the output is 8, the test program passes. If the output is other results, the test program has a problem.

The program class test1 to be tested for package; public class test1 {public int add (int x, int y) {return X + Y;} public static void main (string [] ARGs) {// todo auto-generated method stubint z = new test1 (). add (3, 5); system. out. println (z) ;}} but the above test is too simple and not suitable for large-scale tests. Then, test JUnit introduced in Java to test your program, the main steps are as follows: Create a package named unit test in SRC and right-click unit test --> New --> JUnit test case. The new JUnit test case dialog box is displayed, and name the package for this test: ttest Click Browse... on the right of the class under test line ..., in the "Class under test" dialog box, enter the name of the class to be tested, for example, test1. The class will be located. Click OK and return to the new JUnit test case dialog box, click Next> select the method to be tested, and check and click fishied, the following <PRE name = "code" class = "Java"> package test unit test; import static Org. JUnit. assert. *; import Org. JUnit. test; public class ttest {@ testpublic void testadd () {fail ("not yet implemented ");}}
Then we add some code in it. The test case for the whole test plus the add () method is as follows:
Package test unit test; import static Org. JUnit. assert. *; // static import, which itself is a class, not a package. There are many static methods import Org. JUnit. test; import the program class test1. * to be tested; // import the test1public class ttest {@ testpublic void testadd () class to be tested from a package outside Ning () {// fail ("not yet implemented"); // The static method int z = new test1 () in assert (). add (5, 3); assertequals (8, Z); // assertequals (z, 8); // is equivalent to asserttrue ("z> 10, No, Z is not greater than 10 ", z> 10); // once an error occurs, the content in the double quotation marks will be displayed with a prompt message to facilitate the error. If this line fails, the following will not be checked (executed) // asserttrue (z> 10) ;}} after the above Code is completed, right-click in ttest and choose run as --> JUnit test to test the add () method, if the test is successful, green is displayed. Error: 0; failures: 0.


 


Junit4 unit test

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.