How to unit test through Juit in eclipse

Source: Internet
Author: User
Tags assert apache tomcat

1. What is JUnitJUnit is unit testing, the Unit test framework for the Java language, and is a test of a method of a programIt is generally done by the programmer himself through JUnit, so unit testing is also called programmer testing;If testers are familiar with the programmer's code logic, they can also perform interface tests, which can be used for interface testing, i.e. white-box testing. 2. How to use JUnitto use the JUnit test framework, you have to use it on a MAVEN project basis, so here's a first introduction to MAVEN. ★Maven Introduction 1) is a special Java project, the project created by MAVEN will have a logo labeled M, as shown in. 2) with the JUnit Test package integrated in Maven, it should be said that Maven can integrate anything you want or think of as a tool plugin. 3) Advantage: If you are not a MAVEN project, if you use a jar package, you need to add it manually, but a project created through MAVEN is not required. 4) function: can be compiled, packaged, can also be module management ★ How to create a maven project The first step:Click New->other in Eclipse to create a MAVEN projectStep Two:in the dialog box that opens, select Maven Project, and then click NextStep Three:Tick Create a simple project, then click NextNote:You need to download the first time you use MAVEN, if you have to wait a long time for slow speedFourth Step:in new Maven project, fill in the group ID, artifact ID (folder name), other default or not, and then finishthe group ID and artifact ID are explained as follows:GroupID and Artifactid are collectively referred to as "coordinates" in order to ensure the uniqueness of the project, if you want to get your project to the MAVEN local repository, you want to find your project must be based on these two ID to find. GroupID generally divided into several paragraphs, here I only say two paragraphs, the first paragraph is the domain, the second paragraph is the company name. Domain is also divided into org, COM, CN and so many, including Org is not a profit organization, COM for business organizations. Example of an Apache Tomcat project: The project's GroupID is Org.apache, its domain is org (because Tomcat is a nonprofit project), and the company name is Apache,artigactid is the project name, Tomcat. Fifth Step:Follow the steps above to complete the creation of the MAVEN project. The following is an explanation of the MAVEN project that you created, Test_junit individual folders:1) Src/main/java: Store Java code2) Src/main/resources: A resource file that holds Java3) Src/test/java: Java code to store the test4) Src/test/resources: Storage of some resource files needed to test Java code5) Pox.xml: Some relevant information to describe the MAVEN project, such as: Test_junit version information created, Group Id, package structure, etc., can be found in the pox.xml. Sixth step:how to introduce the JUnit jar package that needs to be used through MAVEN. 1) Open the Pox.xml file and select the Pox.xml tab2) then enter the following code in Pox.xml to introduce the associated JUnit jar package, as follows:
<dependencies> <dependency> <groupId>junit</groupId> <artifactid>junit</ artifactid> <version>4.11</version> </dependency> </dependencies>
Note:This code means to get the code from the central repository and put it in the local repository. the address of the Central warehouse is:mvnrepository.com/artifact/org.hamcrest/hamcrest-core/1.3Local Warehouse View method:windows->preferuserences->maven->user Settingsthen after saving, reopen the Test_junit project and you will see Maven dependeccies, the jar package that was introduced. 3, how to conduct unit testingthere are altogether two methods, respectively:method One:by inheriting the class TestCase, the test method must be named with Test startFor example:Public class Demo1 extends testcase{Public void Test1 () {...}}Note: All methods written in this method must start with test, for example: Testxxxx executing the above code shows whether the two test methods performed successfully on the left. Green represents success, red represents execution failure, and X is displayed above Test1 and Test2 methods. Method Two:you need to add annotations before the test method, and the types of annotations are as follows:@Test: Indicates that this is a test method@Before: The method that is annotated is executed before each test method is executed@After: The method that is annotated is executed first after each test method is executed@BeforeClass: The method that executes the @beforeclass callout when the test case is initialized is executed only once@AfterClass: After all tests have been performed, the execution of the finishing touches, that is, the method to execute the @afterclass annotation, is executed only onceformat:@TestPublic void Method1 () {...}Note:at this time the method name can be casually written, not necessarily written testxxx. For example:
 Public classDemo2 {@BeforeClass Public Static voidMethod5 () {System.out.println ("Demo2.method5 ()"); } @Before Public voidmethod3 () {System.out.println ("Demo2.method3 ()"); } @Test Public voidmethod1 () {System.out.println ("Demo2.method1 ()"); } @Test Public voidmethod2 () {System.out.println ("Demo2.method2 ()"); } @After Public voidmethod4 () {System.out.println ("Demo2.method4 ()"); } @AfterClass Public Static voidMethod6 () {System.out.println ("Demo2.method6 ()"); }}
The following results are performed:Note:@BeforeClass and @afterclass the test method to be defined as a static type to be executed4. Assertion (Assert)unit tests must use assertions, which are used to check for illegal situations rather than error situations, which are illegal situations that should never occur when the program is working properly, to help developers quickly locate the problem. the common APIs are as follows:Assertequals (A, B): Determine if two data is consistentAssertnotequals (A, B): Determine whether two data is inconsistent;Assertnull (object): To see if the object is emptyAssertnotnull (object): To see if the object is not empty;asserttrue (...) : Determines whether the current running result is true;Assertfalse (...) : Determines whether the current running result is false; For example:assertequals (3, Caculator.getresult ());used to determine if the calculator is correctly calculated5. Packaging TestIntroduction:The packaged class test is to package all of the test classes together, i.e. introduce all the test classes before the test method. Advantages:You can test all the methods at once without having to run one at a time. The code format is as follows:@RunWith (Suite.class)@Suite. sutieclasses ({Class 1, Class 2, Class 3, ...})Public class Junittestall {}For example:6. The difference between an assertion and an exceptionassertions are used in things you know will never happen, but because people are always making mistakes, it's not what you want to write. So the assert is used to catch the programmer's own error. However, exceptions (exception) are primarily used to capture user or environment errors.

How to unit test through Juit in eclipse

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.