JUnit---Java Unit test Prerequisite tools

Source: Internet
Author: User
Tags xunit

 

In our daily business code are not finished, why also write test code, after the completion of the project I can not test it?

looks like an increase in the amount of code, but it just helps you reduce the time of the post-test troubleshooting, each team, everyone has a bug indicator, first of all, every time you have so many bug performance is not good to see, and secondly, it just lets us write the code more robust, Improved code quality, late commissioning, and easier maintenance. Imagine, you want to post-overtime debugging, or want to write each business when the correctness test, set aside more time to accompany the girlfriend, with the family more cool   First, JUNIT4 Quick Start 1, JUNIT4 's brief introduction official website: http://junit.org/ JUnit is a simple framework that can write reusable test sets and is a subset of Xunit. So what is Xunit? Xunit is a test-driven development-based testing framework with numerous family members, including Pythonunit, CppUnit (c + +), JUnit (JAVA) JUnit is hosted on GitHub, contains Junit.jar, Hamcrest-core.jar (set matching rules, is JUnit's feature enhancement) two packages           2. Why to use JUnit test framework can help us to write the program for purposeful testing, can help us to minimize the code in the bug, is the program to achieve the desired effect, usually a complete project will contain thousands, or even more methods, then how to determine the results of these methods are what we want, it becomes Important. Of course, you can also create a lot of main functions, use a lot of System.out.println () method to test each method, output the results to the console, but I think no one would like to do such tedious things, and, this way there are obvious shortcomings, the test method can not run together, Most of the results need to be viewed by the programmer himself to determine. Fortunately, JUnit makes all this simple and elegant, and JUnit uses an assertion mechanism that can directly compare our expected results to the final results of the program to ensure the predictability of the results.           3. How to develop test casesThe test framework for JUnit is included in Eclipse Second, JUNIT4 use detailed 1. How to use①, test methods must be decorated with @test ②, test methods must be decorated with public void, with no parameters ③, Create a new source code directory to store our test code, the published project can not contain test code, so in the SRC source code directory to create a new test folder, separate from the project code, when published directly Then delete the test folder. ④, test class packages should be consistent with the class being tested each method in the ⑤, test unit must be independently tested and there can be no dependency between the test methods ⑥, test class uses test as the type suffix (not required) ⑦, test method uses test as the method name prefix (not required) 2. Two cases of test failure failure is generally caused by the failure of the assertion method used by the unit test, which means that the test found a problem, that is, the output of the program is not the same as we expectedError is caused by a code exception, which can result from errors in the test code itself, or it can be a hidden bug in the code being testedtest Cases are not used to prove that you are right, but to prove that you are not wrong. test Cases are only used to achieve the desired results you want, but there is nothing you can do about logic errors. 3. JUnit Run Process① @BeforeClass Modified method is executed before all methods are called, and the method is static, so it runs immediately after the test class is loaded, and in memory he only has one copy, so he It's better to load a configuration file or something that's only suitable for loading once. ② @AfterClass-modified methods are typically used to clean up resources, such as closing a connection to a database ③ @Before and @after are executed once before and after each test methodThe code that is decorated with these four annotations can be understood as fixed code, i.e. code that is bound to be executed 4. Common annotations in JUNIT4 @Test: Modifies a common method into a test method and provides the ability to handle exceptions and time-limited tests @Test (expected = xxexception.class) @Test (timeout= milliseconds), test the dead loop, or perform a simple performance test @BeforeClass: It will be executed before all the methods are run, the static modifier @AfterClass: It will be executed after all methods have been run, static modifier @Before: Executes once before each test method is run @After: Executes once after each test method is run @Ignore: The modified test method is ignored by the test runner @RunWith: You can change the test runner to inherit Org.junit.runner.Runner Assertion third, the deep use of JUNIT4 1. Use of JUnit test suites in the actual project, with the development of the depth of the project will be more and more large, the corresponding test class will be more and more, then you can not run a single, JUnit test suite provides a way to run the batch The ① test suite is the organization of test classes that run together ② writes a class as the entry class for the test suite, which does not contain other methods, changes the test runner Suite.class, and passes the class being tested as an array to Suite.suiteclasses ({}) 2. JUnit parameterization Settings ① Change the default test runner to Runwith (Parameterized.class) ② declaring variables to hold expected and resulting values ③ declares a public static method that returns a value of collection and modifies it with @parameters ④ declares a public constructor with parameters for the test class and assigns a value to the variable in which it is declared Iv. use of JUNIT4 in Web projects integration testing of Spring and hibernate when we use the Open source framework for project development, there is no doubt that the integration between frameworks is a high error rate, and JUnit can be used for the integration testing of spring in Hibernate.  From for notes (Wiz)



JUnit---Java Unit test Prerequisite tools

Related Article

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.