Unit Testing with JUnit

Source: Internet
Author: User

In. NET learning, when exposed to NUnit, to Java Learning, of course, the study of JUnit, this blog brief introduction of JUnit's use.


1. Right-click-->build path-->add Libraries in the project root directory


Figure 1 Adding a jar package

2. Select JUnit under the Add Library and NEXT-->JUNIT4.    

                                                                     


After selecting Finish, you will find that there is one more class library under the project:



3. Tested class

Test object public class Tool {//test method one public int add (int a,int b) {return a+b;} Test method two public int add (int a,int b,int c) {return a+b+c;}}

4. New Test class

Import Org.junit.after;import Org.junit.before;import Org.junit.test;public class Tooltest {/* * use junit Steps * 1. Set up test class Tool Test ==> Specification ==> class name + Test suffix * 2. Guide Package * 3. Create a running test method * 1> creates a method of Publi void, with the method name beginning with test, ending with the method name to be tested (canonical) 2> Enter Note @test */@Testpublic void TestAdd1 () {Tool tool = new tool (); int result  = Tool.add (1, 2);//3system.out.println ( result);} @Testpublic void TestAdd2 () {Tool tool = new tool ();//int i = 1/0;int result  = Tool.add (1, 2,3);//6system.out.println (R Esult);} @Beforepublic void init () {System.out.println ("Call me before each test method runs, initialize some parameters!");} @Afterpublic void Destory () {System.out.println ("Call me at the end of each test method run, release some resources!");}}

5. Select the test method and right-click-->run as-->junit test to run the test


6. The result of the operation is as follows, indicating that the test


Summarize:

The use of JUnit and NUnit is very similar, when we usually do the demo, if we use the main method of the console program, can only execute one thread at a time, if you want to imitate the client to invoke multiple methods can be very troublesome, junit for us to solve the problem well With JUnit, however, testing how many methods, as long as one step, it is very convenient.

Unit Testing with JUnit

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.