Tips for Java test classes (using nested classes)

Source: Internet
Author: User

We know that when writing each class, we need to test whether the class has errors. by writing a main () method in each class, we have a disadvantage, that is, the additional code that has been compiled must be carried with it. If this is troublesome for you, you can use Nested classes to place the test code.

/***** @ Author xiaanming **/public class innerclass {public void print (Object O) {system. out. println (O. tostring ();} public static class tester {public static void main (string [] ARGs) {innerclass minnerclass = new innerclass (); minnerclass. print ("xiaanming ");}}}

In this way, an independent class innerclass $ tester is generated (to run this program, execute Java innerclass $ tester and escape $ in Unix/Linux systems) you can use this class for testing, but you do not need to include it in your released product. before packaging the product, you can simply delete innerclass $ tester. class. In this way, you can test every class without any additional code in the packaged product. Is it very convenient? Haha

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.