Java Unit Test JUnit (i) JUnit introduction and use of __java

Source: Internet
Author: User

Why do you write unit tests?
Enterprise development, regardless of the size of the project are inseparable from testing, including unit testing, regression testing, performance testing and so on, and unit testing is to verify that the programmer is the correct way to write code, in the day-to-day development of unit testing is very necessary, imagine if you are developing a huge project, If each of the business logic written by the deployment to the server to run the program through the front interface click to test, first: inefficient, not automated, second: If a certain day a new code added to the bug, find a mistake is a lot of kung fu, So I suggest that developers should be proficient in the preparation of unit testing

What is JUnit?
JUnit is a very high level of Java Domain Unit Testing framework, has become almost the standard unit test, of course, JUnit also has some shortcomings, this article starts with the simple use of junit

The principle of unit test writing:
1. Create 1 source folder in Eclipse named Test (created after using MAVEN)
2. The package requirements for the test class are the same as those of the tested class
3. Test class to use test as the beginning or end, such as--> userservicetest
4. Each method of testing a class must be performed independently, without order or dependency

The difference between JUNIT3 and JUNIT4:
In JUnit3, the test class needs to inherit the TestCase, and the test class needs to use TESTXXX as the beginning, and if you want to run an initialization method before testing the method, the name of the method must be setup, and running a method name after testing the method must be teardown
Instead of inheriting testcase in the JUNIT4, it uses a more convenient annotation, which only needs to be represented by the @test, which uses @before to initialize the method before the test, and to release the resource method using @after after the test, if you only want the method to execute only once, Using @beforeclass and @afterclass

JUnit uses:
Because the difference between JUnit3 and JUNIT4 is more obvious, so I use JUNIT4 here to explain

package com.accentrix.ray;     import org.junit.after;   import org.junit.afterclass;   import org.junit.before;   import org.junit.beforeclass;   import org.junit.test;    //   *  test class most basic format    */  public class testuser {         @BeforeClass       public static void init ()  {          /*             *  This method executes only one,  before running all unit tests             *  typically uses to get database connections, spring-managed beans, and so on            */     &NBSP}          @Before       public  Void setup ()  {          /*    &NBsp;       *  This method executes,         before running each unit test      *  data or objects that are typically used to prepare data or get unit tests to be relied upon             */     &nbsp          @Test        public void testadduser ()  {          /*   The main method of          *  test class, where to write all the test business logic             */     &nbsp          @Test (expected = exception.class)       public void testdleteuser ()  {          /*             *  Please note that the experced on the annotation, using this parameter representative can be considered               *  This unit test method throws exception exception if not considered not to pass            */      &nbsp          @Test (timeout = 1000)        public void testupdateuser ()  {           /*            *  Note the timeout on the note, using this parameter to represent the unit test            *  complete in 1000 milliseconds, otherwise it will be used as a simple performance test             */      }           @After       public void teardown ()  {           /*            *  When this method runs each unit test, it executes,             *  is primarily used to correspond to the setup, to clean up the acquired resources            */      }           @AfterClass       public static void destroy ()  {           /*             *  This method executes one,  after all unit tests have been run            *  Typically used to release resources such as database connections, IO streams, etc.  
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.