Java Unit Test

Source: Internet
Author: User
Tags assert naming convention

Unit Test1. Introduction

In everyday development, any code that we write requires rigorous testing before it can be published. The previous test method is to write a main function for a simple test, and use a large number of print statements output results, this method is not desirable, it will lead to a large number of redundant code in the program, and is not conducive to maintenance. Therefore, the concept of unit testing is put forward by the industry. The so-called unit test is to check and verify the smallest testable test in the software. (The smallest unit can be a method or a class, defined according to a specific scenario). In Java, the JUnit tool is typically used to accomplish this.

2. Use

Import the downloaded JUnit jar file into your project. Next you can write the unit test class, and the test class has a corresponding naming convention. Usually named in the Xxxtest Way, XXX is the class name you want to test. Any test method written in the test class is not required to return the merit. Test Method Name Specification testxxx (), XXX represents the method name in the target class.

2.1 JUnit's annotations
Annotations Description
@Test Labeling on a method, indicating that the current method is a test method
@Before The method that is labeled on the method that is used before any unit tests are executed
@After A method that is labeled on a method for execution after any unit test is executed
@BeforeClass Labeling on a static method, executed first before the method is tested, and executed once. Typically available for initialization operations
@AfterClass Labeling on a static method, executed first before the method is tested, and executed once. Typically available for initialization operations
@Ignore Label on the method, indicating that the current method is ignored

Example:

 Public classuserdaotest {? @BeforeClass Public Static voidBefore1 () {System.out.println ("Before1");    }? @AfterClass Public Static voidAfter1 () {System.out.println ("After1");    }? @Before Public voidBefore2 () {System.out.println ("Before2");    }? @After Public voidAfter2 () {System.out.println ("After2");    }? @Test Public voidTestsaveuser () {Userdao DAO=NewUserdao ();    Dao.saveuser ();    }? @Test//@Ignore//ignore the current method     Public voidTestfinduserbyid () {Userdao DAO=NewUserdao ();    Dao.finduserbyid (); }}

                  < Span class= "cm-variable" >  < Span class= "Cm-keyword" >     
?
2.2 using Assertions (assert)

The assertion is that the expected condition or expression is involved in the test to determine whether the result of the test achieves the desired result.

Example:

ImportEdu.nf.service.UserService;Importorg.junit.Test;//use static import (a static method is imported)Import Staticorg.junit.assert.*;?? Public classuserservicetest {? @Test Public voidTestfindusername () {UserService service=NewUserService (); //using assertions for testing//The first parameter is the result of this expected return, and the second is the return value of the target method//if two values are equal, the test passes aAssertequals ("HH", Service.findusername (1001)); }}?

The Assert class provides a number of assert methods to compare, and the return values of these methods are void, and the corresponding exception information is thrown if the result of the decision is not passed. Common assertion methods are as follows:

method description
assertequals Determines whether two values are equal
Assertnotnull decision result not allowed to empty
assertnull Null decision result
assertsame to determine whether two objects are the same reference
assertarrayequals determines whether the contents of two arrays are equal
... ...
2.3 Test Kit

The so-called test suite is a series of unit test classes that are assembled together for batch unit testing.

Notes required for the test suite:

Annotations Description
@RunWith Label on the class, indicating that the current class is a test suite runner
@SuiteClasses Label on class, used to centralize all class of unit test classes

Example:

@RunWith (Suite.  Class) @Suite. suiteclasses ({userdaotest. class, Userservicetest. class })publicclass  suitetest {}?

Java Unit Test

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.