Summary of JUnit usage

Source: Internet
Author: User
The typical method to use JUnit is to inherit the testcase class, and then reload some of its important methods: setup (), teardown (), runtest () (these are optional ), testcase automatically creates and destroys fixture through setup and teardown. It calls setup before running each test, and CALLS teardown after each test is complete. The testcase class also implements the assert class, which defines eight core methods. There are also two important interfaces: Test and testlistener. 1. test contains two methods: Run () and counttestcases (), which are used to extract test action features. 2. testlistener contains four methods: adderror (), addfailure (), starttest (), and endtest (). It processes the test results and extracts the action features of the test-driven process. All testcase classes need to define a testsuite. If no testsuite is defined for testcase, testrunner will define a testsuite for the testcase class. If no, JUnit. swingui. testrunner creates a default testsuite for testcalculator. The default testsuite adds all tests named test *** in testcalculator to the testsuite Based on the reflection mechanism. Then testrunner calls run to execute all testcase in the testsuite. You can also define your own testsuite and write the testcase or testsuite to be tested in the Custom testsuite. Finally, these customer objects are assembled into a testsuite object, which is driven by JUnit. textui. testrunner. Run (case set.

Public class mathtooltest extends testcase {// The class containing the test case inherits public mathtooltest (string testmethod) from the testcase class {// The constructor super (testmethod) of the class );} public void testgcd () {// a test case assertequals (5, mathtool. gcd (10, 5);} public static void main (string [] ARGs) {// main function of the class JUnit. textui. testrunner. run (mathtooltest. class) ;}} if you do not provide any testsuite, testrunner will create one by itself, and then the testsuite will use reflection (reflection) from the run. automatic Find the testxxx () method and run it. Public static void main (string [] ARGs) {JUnit. textui. testrunner. run (mathtooltest. class);} if you want to generate your own testsuite, you can use the static suite Method After inheriting the testcase class and add a test case for this class, in this case, it is not the default suite created by testrunner, so that the test case does not have to start with test, because it is not implemented using the reflection mechanism. in class, find the testxxx test case for execution. (But this method is not recommended) use the following public class mathtooltest extends testcase {public mathtooltest (string testmethod) {super (testmethod);} public void testgcd () {assertequals (5, mathtool. gcd (10, 5);} public static test suite () {// inherits testcase and uses the suite method to construct your own suitetestsuite suite = new testsuite (mathtool. class); // This suite has only one. classsuite. addtest (New mathtooltest ("testgcd"); // Add the mathtooltest class to this suite. // testgcd Test Case return suite; // return this suite} public static void main (string [] ARGs) {JUnit. textui. testrunner. run (mathtooltest. class); // This is the execution of the above-generated suite}. You can also use the static Suite () method after inheriting testsuite to integrate multiple. the class file is a suite, because each. the class contains multiple test cases, so it is considered as a suite (this suite can be automatically mapped, which is recommended; you can also use the suite Method After inheriting the testcase class. manually add the generated suite to the class, as shown above) the public class testall extends testsuite {// self-constructed suite class inherits the public static t from the testsuite class Est Suite () {// use the static method suite to construct your own suitetestsuite suite = new testsuite ("testsuite test"); // name your own suite name suite. addtestsuite (testcalcuator. class); suite. addtestsuite (testcalcuator2.class); Return suite;} public static void main (string ARGs []) {// JUnit. textui. testrunner. run (Suite (); JUnit. textui. testrunner. run (testall. suite () ;}} both testcase and testsuite implement the test interface, and the running mode is an instance in command mode, while testsuite can be combined Testsuite or testcase. This is an instance in composite mode.

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.