Setup and tear-down of JUnit

Source: Internet
Author: User

Before JUnit runs each test... method, it calls setup (). After each test method is complete, it calls teardown (). Therefore, you can set and clean up the test environment and release resources in these two methods, such as opening and closing the database connection.

 

Public class testdb extends testcase ...{

Private connection dbconn;

Protected void setup ()...{

Dbconn = new connection ("oracle", 1521, "XXX", "XXX ");

Dbconn. Connect ();

}

Protected void teardown ()...{

Dbconn. Disconnect ();

Dbconn = NULL;

}

Public void testxxx ()...{

...

}

Public void testyyy ()...{

...

}

}

Similarly, setting up the environment for the entire test suite and releasing resources can also be placed in per-Suite setup and per-Suite tear-down. To implement per-Suite setup and per-Suite tear-down, you need to package the suite to be tested into a testsetup object.

 

Import JUnit. Framework .*;

Import JUnit. Extensions .*;

Public class testclasstwo extends testcase ...{

Public testclasstwo (string method )...{

Super (method );

}

Public void testxxx ()...{

...

}

Public void testyyy ()...{

...

}

Public Static Test Suite ()...{

Testsuite suite = new testsuite ();

Suite. addtest (New testclasstwo ("testxxx "));

Testsetup wrapper = new testsetup (suite )...{

Protected void setup ()...{

Onetimesetup ();

}

Protected void teardown ()...{

Onetimeteardown ();

}

};

Return wrapper;

}

Public static void onetimesetup ()...{

Dbconn = new connection ("oracle", 1521, "XXX", "XXX ");

Dbconn. Connect ();

}

Public static void onetimeteardown ()...{

Dbconn. Disconnect ();

Dbconn = NULL;

}

}

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.