JUnit Study Notes (1): JUnit installation and testing principles

Source: Internet
Author: User
Here is an example of addition, subtraction, multiplication, and division. The final project is as follows: 1. Import the JUnit jar package. We do not recommend that you directly use the JUnit jar package provided in eclipse to download it: Click to open the link. The latest version is 4.11. Pay attention to one problem: If version 4.11 is used, you need to download hamcrest 1.3 at the same time.-> click the open link to import both packages at the same time !! In the previous version, you only need to import a JUnit jar package. 2. Write the service code in the src directory. Here is a class with only the addition, subtraction, multiplication, division, and other functions.
package com.fjnu.util;public class Calculate {public int add(int a, int b){return a+b;}public int minus(int a, int b){return a-b;}public int divide(int a,int b){return a/b;}public int multi(int a, int b){return a*b;}}

3. Basic principles for creating a test class: 1. Create a source folder named test in eclipse. 2. Create a test class package. The package name is the same as the class to be tested.

Package COM. fjnu. util; import Org. JUnit. before; import Org. JUnit. test; // static Import static Org. JUnit. assert. *; public class testcalculate {private calculate Cal; // The setup method @ beforepublic void setup () {Cal = new calculate ();} will be executed before any method is executed ();} // @ test is added to indicate that this method is a unit test @ testpublic void testadd () {/** The following is the compilation of a simple assertion * The first parameter is the prompt message provided if an error occurs * The second parameter indicates the expected value, it is usually the content specified by the user * The third indicates the actual value returned by the Code * // assert. assertequals ("problem with addition", 34, Cal. add (12, 22 )); /** because the package has been statically imported * All static methods in assert do not need to add class names * This can effectively be compatible with junit3 */assertequals ("there is a problem with addition ", 34, Cal. add (12, 22);} @ test public void testminus () {int res = Cal. minus (12, 2); // assert. assertequals ("subtraction has a problem", 10, Res); assertequals ("subtraction has a problem", 10, Res);} // indicates that this test class should throw arithmeticexception, if no error is thrown, @ test (expected = arithmeticexception is returned. class) Public void testdivide () {int res = Cal. divide (12, 0); assertequals ("division problem", 6, Res);} // The unit of time is ms @ test (timeout = 190) Public void testtime () throws interruptedexception {thread. sleep (209); int rel = Cal. divide (12, 2) ;}@ test public void testmulti () {int res = Cal. multi (12, 2); assertequals ("multiplication problem", 24, Res);} // hamcrest 1.3}
4. The difference between junit3 and junit4 is very obvious in junit3. If a class needs to be a test class, it must be inherited from testcase. If a method needs to be a test method, the method must start with testxxx. In junit3, if you want to specify a test method to run an initialization method before running, the method name must be setup, if you want to run a method to release resources after a test method is run, the name of this method must be teardown in junit4, and a pojo class is a test class, the test method is identified by @ test, the initialization method is identified by @ before, and the method for releasing resources is marked by @ after. However, to enable the test class in junit4 to be used in junit3, we are used to naming the initialization method setup and the method for releasing resources as teardown, the test method also starts with test. 5. How to Use assertions to provide an Assert class in junit4? There are a lot of methods in this class to process assertions, in junit3, because testcase is inherited, this testcase provides a large number of assert methods. In junit4, to be compatible with junit3, the static import package assert, that is, import static Org. JUnit. assert. * In this case, you do not need to add a class name when using all the static methods in assert. It can be effectively compatible with the junit36 and expected parameters and test the exception object: @ test (expected = arithmeticexception. class) to check whether arithmeticexception is thrown.
// Indicates that this test class should throw arithmeticexception. If no arithmeticexception is thrown, an error is returned: @ test (expected = arithmeticexception. class) Public void testdivide () {int res = Cal. divide (12, 2); assertequals ("Division problems", 6, Res );}
7. Timeout parameter. Performance Test: @ test (timeout = 190), which indicates that the task must be completed within milliseconds.
// The time unit is ms @ test (timeout = 190) Public void testtime () throws interruptedexception {thread. sleep (189); int rel = Cal. divide (12, 2 );}
 

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.