JUnit Simple Instances and templates

Source: Internet
Author: User
Tags assert exception handling

The development process of unit testing, as early as possible to find bugs, developers should do. JUnit provides a good test mechanism for Java developers, as follows is a simple example of a junit application and a junit template for beginners to reference.

Package junit.sineat.templet;
Import java.util.Hashtable;
Import Junit.framework.Assert;
Import Junit.framework.TestCase;
Import Junit.framework.TestSuite;
public class JUNITB extends TestCase ... {
/** *//** defines the classes you need to test and the variables you use *****************************/
Public Hashtable Hasha=null; //
Public Hashtable Hashb=null;
/** *//*******************************************************/
Public JUNITB (String name) ... {
Super (name); To create a child class
}
/** *//** Use Setup to initialize the operation.
protected void SetUp () throws Exception ... {
Super.setup ();
Hasha =new Hashtable (); Over here
}
/** *//** use teardown to destroy the resources they occupy.
protected void teardown () throws Exception ... {
Super.teardown ();
System.GC ();
}
/** *//** Write a test method to assert the desired result **/
public void Testbodystatus () ... {
Hasha =new Hashtable (); After this sentence can also be removed from the setup () teardown ()
Assertnotnull (Hasha);
Hasha.put ("0", "let ' s try again"); Test1.error Edition
Asserttrue (Hasha.isempty ()); Expected to be empty
}
/** *//** Write a test method to assert the desired result **/
public void Testbodysame () ... {
hashb= (Hashtable) Hasha.clone (); Test2.error Edition
Hashb=hasha; Test2. OK version
Assert.assertsame (HASHA,HASHB);
}
/** *//**suite () method, using reflection to dynamically create a test suite that contains all the testxxxx methods **/
public static TestSuite Suite () ... {
return new TestSuite (Junitb.class);
}
/** *//**** Write a main () Run test *****************/
public static void Main (String args[]) ... {
Junit.textui.TestRunner.run (Suite ()); In the way of the text-running device
Junit.swingui.TestRunner.run (Junitb.class);
}
}

The above is a simple test sample of junit, without much consideration for performance and specification, but senior Java developers recommend:

First, do not initialize in the constructor of the test case

The answer is to initialize the Setup () method of the overloaded test case.

Second, do not assume that the test in test cases in the order of execution

A good habit is to maintain the independence between tests so that the results are the same in any order.

Third, the test should avoid the artificial intervention

Experience two is about different tests to avoid correlation, and experience three is actually testing to avoid autocorrelation.

To invoke the Setup () and teardown () of the parent class in the subclass

V. Do not rigidly specify the path of the data file

Put the test code and the measured code in the same directory

Seven, correct name test

To consider regional and national settings when writing tests

Use JUnit's automatic exception handling to write concise test code

In fact, it is not necessary to use Try-catch in JUnit to catch exceptions, and JUnit will automatically catch exceptions. Exceptions that are not caught are treated as errors.

Ten, make full use of the Assert/fail method of JUnit

Assertsame () is used to test whether two references point to the same object

Assertequals () is used to test whether two objects are equal

Xi. ensure that the test code is not time dependent

12, use the document Builder to do the test document.

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.