Java Knowledge Accumulation-unit test and JUnit (i)

Source: Internet
Author: User
Tags set time

When it comes to unit tests, people who have just graduated or never graduated may be mostly stuck in the definition of the textbook, and as to how it is defined, it is not expected that too many people will remember. Our education always makes people "happy". So what is unit testing? The specific scientific definition I do not care about, in fact, every person who writes code in the moment to carry out unit testing, unless you never verify that the code you write can achieve the intended purpose, but directly after the completion of the finished son, even run a bit do not proceed.

Unit testing is straightforward to verify that a piece of code is written correctly, possibly a class, possibly a function, or even a loop. For the convenience of testing, we typically test directly in a well-written, running code, and see that the console outputs the desired result or throws an exception. But into the company, such a test method is very unprofessional, it is not easy to reproduce the test environment. Then you might want to write a test class specifically, call the code unit you want to test, and then test it. But it's a hassle to write test cases every time you need to create a new class.

Having said so much, the welfare came. Unit Test Tool--junit. Assist us with unit testing and provide many conveniences. Here's how to use JUnit for unit testing in eclipse.

If we write a calculator, we provide some simple calculator functions. The code below, deliberately leave some bugs.

1PackageOrg.logback.test;23PublicClassCalculator {45PrivateStaticIntResult6//Static variables for storing run results78Publicvoid Add (IntN) {9Ten result = result +N1112}1314Publicvoid Substract (IntN) {15result = Result-1;//Bug: The correct one should be result = Result-n1718}1920Publicvoid Multiply (IntN) {21st22}//This method has not been implemented2324Publicvoid Divide (IntN) {25result = result/N2728}2930Publicvoid Square (IntN) {31result = n *N3334}3536Publicvoid SquareRoot (IntN) {3738for (;;);//Bug: Dead Loop3940}4142public void Clear () {//  clear 0 43 44 result = 0< Span style= "color: #000000;" >; 45 46 }47 48 public int GetResult () {49 50  Return Result; Wuyi 52 }53}   

Then to the class under the existing development level of unit testing, the method is very simple, first in the class belongs to the project name right-click into the properties.

In the Properties window that pops up, choose Java Build Path on the left, then select the Libraries tab on the right, and finally select Add Library.

In the Popup Add Library window, select JUnit to click Next, then select JUnit4 in the dropdown menu and click Finish.

In this way JUnit is introduced into the project's libraries, click OK. Right-click on the class that you want to test, and select New→junit test case.

In the new window that pops up, set it up as follows, and the meaning is explained later

Here you can choose to put the test class in a special test package, and give the test class an obvious name. Select Next and then enter.

Here is the choice to test the method, if we only test subtraction 4 methods, then tick the corresponding 4 methods, click Finish. A test class is generated automatically, but we still need to make some modifications to this class to finally modify the test class to the following code

1PackageOrg.logback.test;23ImportStatic org.junit.assert.*;45ImportOrg.junit.After;6ImportOrg.junit.AfterClass;7ImportOrg.junit.Before;8ImportOrg.junit.BeforeClass;9ImportOrg.junit.Ignore;10ImportOrg.junit.Test;1112PublicClasscalculatortest {1314PrivateStatic Calculator example =NewCalculator ();15@Before//Methods to execute before each test method is executed17Publicvoid SetUp ()ThrowsException {18Example.clear ();19}20@After//A method that executes immediately after each test method is executed22PublicvoidSetdown () {System.out.println ("Over");24}25@BeforeClass//The method that is called when the class is loaded must be public and static, only one call at a time27PublicStaticvoidStart () {System.out.println ("Start class"));29}30@AfterClass//The method that is called at the end of the class must be public and static, only called once32PublicStaticvoidDestory () {System.out.println ("Destory class");34}3536@Test37PublicvoidTestadd () {Example.add (2);Example.add (3);Assertequals (5, Example.getresult ());41}4243@Test44PublicvoidTestsubstract () {Example.add (10);Example.substract (2);Assertequals (8, Example.getresult ());48}49@Ignore//Ignore test because the method is not implemented yet51@Test52PublicvoidTestmultiply () {Yet fail ("not implemented");54}5556@Test57PublicvoidTestdivide () {Example.add (8);Example.divide (2);Assertequals (4, Example.getresult ());61}62@Test (timeout = 1000)//Set time limit in milliseconds, time-out to count test failure64PublicvoidTestsquareroot () {65 example.squareroot (4}68 69 @Test ( expected = ArithmeticException. class) // Test if you can throw the exception as expected 70 Span style= "color: #0000ff;" >public void DivideByZero () { 71 example.divide (0); //72 }73}     

Java Knowledge Accumulation-unit test and JUnit (i)

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.