Initial Knowledge of Junit unit testing, and junit Unit Testing

Source: Internet
Author: User

Initial Knowledge of Junit unit testing, and junit Unit Testing

The children's shoes that have written unit tests are no stranger to Junit, but I am a little bit white. I am just getting started with them. Here I will record my test labs for future convenience. I learned how to use JUnit4. Since the latest version is used, I should not consider how to use the old version. What is JUnit? Is a plug-in for testing! Download the latest JUnit plug-in from the JUnit official website.

  

The annotation is used here. It is easy to understand as dependency reflection. Just Mark @ Test before the Test class. I have read that in JUnit3, the name of the Test method must be identified by testxxx, it also needs to inherit TestCase and so on. JUnit4 uses a large number of annotations to solve this problem, which is convenient and fast.

The following describes some frequently used Annotations:

@ Before: Initialization Method

@ After: release resources

@ Test: Test method. Here we can Test the expected exceptions and time-out.

@ Ignore: Test Method ignored

@ BeforeClass: All test methods are executed only once and must be static void

@ AfterClass: only one test is executed and must be static void.

@ RunWith: specifies that the test class uses a runner.

@ Parameters: Specifies the test data set of the test class.

@ Rule: allows flexible addition or redefinition of the behavior of each test method in the test class.

@ FixMethodOrder: Specifies the execution sequence of the test method.

A simple test class:

Public class JunitTest {@ BeforeClass // all test methods are executed only once and must be static void public static void beforeClass () {System. out. println ("BeforeClass");} @ Before // initialization method public void before () {System. out. println ("Before") ;}@ Test // test method. Here we can Test the expected exception and time-out public void test () {System. out. println ("basic Test") ;}@ Test // Test method. Here we can Test the expected exception and time-out public void test1 () {System. out. println ("basic Test 2") ;}@ Test public void test2 () {StringBuilder sbu = new StringBuilder (); StringBuffer sbf = new StringBuffer (); for (int I = 0; I <100; I ++) {if (I % 2 = 0) {sbu. append (I + "");} else {sbf. append (I + "") ;}} try {Thread. sleep (1000);} catch (InterruptedException e) {e. printStackTrace ();} System. out. println ("an even number less than 100:" + sbu. toString (). trim (); System. out. println ("an odd number less than 100:" + sbf. toString (). trim () ;}@ Ignore // test method ignored public void Ignore () {System. out. println ("Ignore") ;}@ After // release the public void after () {System. out. println ("After") ;}@ AfterClass // For all tests, run only once and must be static void public static void afterClass () {System. out. println ("AfterClass ");}}

  

 

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.