The Java language Ecology article Junit test Unit

Source: Internet
Author: User
Tags mul

01 Overview
JUnit is a regression testing framework (regression testing framework) written by Erich Gamma and Kent Beck. The JUnit test is a white-box test. JUnit has its own JUnit expansion biosphere. Most Java development environments have integrated JUnit as a unit test tool. [1]

JUnit is an open-source Java test Framework for writing and running repeatable tests. He is an example for the xunit of the Unit Test framework system (for the Java language). It includes the following features:

1. Assertions for testing desired results (assertion)

2. Test tools for sharing common test data

3. Test suite for convenient organization and run test

4. Test runner for graphics and text

Testing is generally divided into: unit testing, integration testing (mainly see a piece of code added, the system will not have problems), acceptance testing and stress testing. At the big point of the company developers work every day, the first thing is to checkout down the code of their own responsibility from SVN, and then run the unit test, if the unit test passes, then the code is not a problem, then the code block to modify and add, complete and then test with JUnit, If there is no problem after the test is completed, then submit the corresponding code block to SVN. [2]

The JUnit configuration

Environment: JUnit is configured in Eclipse,

    1. Select Project JUNIT4 (in Package explorer)
    2. Right-click the mouse
    3. Select Properties
    4. Select Java Build Path (in the left menu)
    5. Select the label on the right Libraries
    6. Click the button "Add Library"
    7. Select JUnit, click the button "Next>"
    8. Select JUnit Library version as: JUNIT4
    9. Click the button "Finish"
    10. Click the button "OK"

or manually add Junit.jar and Hamcrest-core-xx.jar,hamcrest-library-xx.jar (or Hamcrest-all-xx.jar). (Recommended for manual addition, only Junit-xx.jar and Hamcrest-core-xx.jar are introduced in eclipse through the previous method)

The difference between JUNIT3 and JUNIT4

    1. Junit3 must introduce and inherit TestCase
    2. The JUNIT3 test method must begin with testing
    3. Junit3 the method of execution before and after the test method must be named Setup () and TearDown ()
    1. JUNIT4 does not need to introduce and inherit testcase. Use annotations to add @test before the method
    2. JUNIT4 test method does not need to be forced to start for test
    3. JUNIT4 test methods before and after execution methods do not need to be forced to name setup () and TearDown (), just add annotations @before and @after
    4. Exception test added in JUNIT4
Several common annotations of JUNIT4 [3]

The sequence of unit test case execution for Junit 4 is:

@BeforeClass –> @Before –> @Test –> @After –> @AfterClass;

The order in which each test method is called is: @Before –> @Test –> @After.

--------------------------------------------------------------------

    1. @Before: Initialization method, code that must be executed before any test execution;
    2. @After: Frees up resources to perform the finishing touches after any test execution. Once each test method executes, the annotation can only modify the public void method;
    3. @Test: The test method indicates that this is a test method. will be executed automatically in JUnit. The annotation can only modify the public void method. The declaration of a method has the following requirements: The name can be arbitrarily taken without any restrictions, but the return value must be void and cannot have any arguments. If these rules are violated, an exception is thrown at run time. As for what to write in the method, it depends on what you need to test; Here you can test for expected exceptions and timeouts, such as @Test (timeout = 100): We set a execution time for the test function that exceeds this time (100 milliseconds) and they are forcibly terminated by the system. And the system will tell you that the function ends because of a timeout, so you can spot the bugs.
    4. @Ignore: Ignored test methods, the meaning of the label is "some methods have not been completed, not participate in this Test", so the test results will indicate that you have a few tests are ignored, not failure. Once you have completed the corresponding function, just delete the @ignore annotation and you can perform the normal test.
    5. @BeforeClass: For all tests, execute only once and must be public static void;
    6. @AfterClass: For all tests, it will be executed once at the end of all test method execution and must be public static void;
Hamcrest and TestSuite introduction [4]

Hamcrest is a framework that is specifically designed to enhance junit. In JUnit, the main use of assert is to make some basic judgments. However, many times it is not convenient to use Assert to judge, if you want to determine whether a certain value is true or FALSE, then use Hamcrest to judge the convenience of many.

There may be a lot of test classes in a project, and if every test class needs to be clicked, then hundreds of thousands of classes will need to be tested, which can be a heavy task, and this could be solved by using the suite provided in the jar package that can be used with JUnit.

06 General Examples

Testcalcuate class

Import static Org.junit.assert.*;import static Org.hamcrest.matchers.*;import Org.junit.before;import org.junit.Test ;p Ublic class Testcalcuate {calcuate cal; @Beforepublic void SetUp () {cal=new calcuate ();} @Testpublic void Testadd () {int rel=cal.add (n), assertequals ("additive existence problem", rel, 55);} @Testpublic void Testminus () {int Rel=cal.minus (one), Assertequals ("subtraction exists", rel,22);} @Test (expected=arithmeticexception.class) public void Testdivid () {int rel=cal.divide (0); Assertequals ("There is a problem with division", rel,2);} @Testpublic void Testmul () {int Rel=cal.mul (ten); Assertequals ("There is a problem with multiplication", rel,200);} @Test (timeout=1000) public void Testtime () {try{thread.sleep (200);} catch (Exception e) {e.printstacktrace ();} int Rel=cal.mul (ten); Assertequals ("There is a problem with multiplication", rel,200);} @Testpublic void Testham () {assertthat (40, GreaterThan (+), LessThan)), Assertthat (AllOf (AllOf (LessThan)), Assertthat ("Like.txt", EndsWith (". txt"));}}

Testa class

Import org.junit.*;p Ublic class TestA {@Testpublic void TestA () {System.out.println ("TestA");}}

Testb class

Import org.junit.*;p Ublic class Testb {@Testpublic void Testb () {System.out.println ("Testb");}}

Testsuite class

Import Org.junit.test;import Org.junit.runner.runwith;import Org.junit.runners.suite;import Org.junit.runners.Suite.SuiteClasses, @RunWith (suite.class) @SuiteClasses ({testa.class,testb.class, Testcalcuate.class}) public class TestSuite {@Testpublic void TestSuite () {System.out.println ("TestSuite");}}

07 Other questions

1. Test principle:[4]

    1. It is recommended that you create a dedicated source folder--->test to write test class code. The above example creates a new resource bundle for test.
    2. The package for the test class should remain consistent with the class that needs to be tested. In the example above , the Calcuate class is in the Src/cn/whp/util package, and the corresponding test class is in test/cn/whp/util.
    3. Each test method in a test cell must be independently executed, not interdependent, and without order.

2, using JUnit in unit testing, the use of the Assertthat method, found that hamcrest GreaterThan and other methods do not recognize "said there is no definition", and cannot be statically referenced to the matchers class "import static org.hamcrest.matchers.*;" [5]

  Cause: Because JUnit relies on hamcrest-core-1.3 packages by default , there is actually a Hamcrest-library.jar for hamcrest to place a lot of tools

Reference documents

[1] The Baidu encyclopedia Junit

[2] Regret Sea http://huihai.iteye.com/blog/1986568

[3] Xiao Qian's blog http://blog.sina.com.cn/s/blog_8354dda80101ee8v.html

[4] Regret Sea http://huihai.iteye.com/blog/1994270

[5] http://www.fwqtg.net/junit-uses Hamcrest's matcher to find problems (such as GreaterThan). html

The Java language Ecology article Junit test Unit

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.