JUnit Test in Java

Source: Internet
Author: User

JUnit Test in Javaquestion one:when to use the Asserttrue,assertfalse statement in JUnit
Asserttrue (Boolean condition);

Condition: If the condition result is true, pass the test.

Assertfalse (Boolean condition);

Condition: If the condition result is false, pass the test.

question two: Java uses JUnit test instances

Reference:

How Java uses JUnit for unit testing-fench-Blog Park
Http://www.cnblogs.com/fench/p/5936008.html

What is unit testing?

The explanation for the Baidu Encyclopedia is this: unit testing (module testing) is a small piece of code written by a developer to verify that a small, well-defined function of the code being tested is correct. Typically, a unit test is used to determine the behavior of a particular function under a particular condition (or scenario). For example, you might put a large value in an ordered list, and then confirm that the value appears at the end of the list. Alternatively, you might remove characters from the string that match a pattern, and then confirm that the string does not actually contain the characters anymore.

In short, unit testing is a test of the smallest functional module in your program, which may be a function in the C language, or a method or class in Java.

The goal is to improve the quality of the code.

What is JUnit?

JUnit is a unit test framework for the Java language. Founded by Kent Beck and Erich Gamma, it became the most successful of the Xunit family of Sunit from Kent Beck. JUnit has its own JUnit expansion biosphere. Most Java development environments have integrated JUnit as a unit test tool.

In other words, JUnit is a well-written unit test framework, using this framework you can greatly shorten your test time and accuracy (I now remember the first time, the C language of the small program, each time is a restart test, the kind of compilation-input-stop-compile the bitter days, very painful, This unit test framework is much better today with JUnit).

Note: Eclipse already comes with JUnit, the current common version of JUnit is divided into 3.X (need to be added manually), 4.X (support annotations)

My version of Eclipse comes with junit3.x, and this article takes junit3.x as an example to test.

The following is a simple example of what a unit test is and how JUnit can get started, using the "sum of maximal subarray" examples.

1  PackageEdu.sau.maximun;2 3  Public classSubarray {4   //maximum of two numbers5    Public intMaxintXinty) {6       if(x>y)7           returnx;8       Else9           returny;Ten   } One    A   //The sum of the largest sub-arrays -    Public intMaxsubarr (int[] arr) { -       intsum = arr[0];//the maximum sub-array and the       intSub_sum = arr[0];//Middle Value -        -        for(inti = 1; i<arr.length; i++){ -Sub_sum = Max (arr[i],sub_sum+arr[i]); +sum =Max (sub_sum,sum); -       } +       returnsum; A   } at}

The above code implements the "Maximum subarray of the sum" algorithm, there are two methods of Max and Maxsubarr, the following start testing (re-create a new package, named Test, all tests are again this new package completed)

1  Packageedu.sau.maximun.test;2 3 ImportEdu.sau.maximun.SubArray;4 Importjunit.framework.TestCase;5 6  Public classTestmaximunextendsTestCase {7     8     //to test the method Max9      Public voidTestmax () {Ten         intx = 1;//test Data One         inty = 2; A         intz =-1; -Subarray Sub =NewSubarray (); -         intRESULT1 =Sub.max (x, y); the         intRESULT2 =Sub.max (y,x); -         intRESULT3 =Sub.max (z, x); -Asserttrue (RESULT1 = =y); -Asserttrue (RESULT2 = =y); +Asserttrue (RESULT3 = = 1); -     } +      A     //Test the method Maxsubarr at      Public voidTestmaxsubarr () { -          -         intArr1[] ={1,2,3,-1};//Test Cases -         intArr2[] = {1,2,-4,8,4,-4,6,-2,1}; -         intArr3[] ={-4,-2,-1,-3}; -         intArr4[] = { -4,2,-4,2,-1}; inSubarray Sub =NewSubarray (); -          to          +Asserttrue (Sub.maxsubarr (arr1) = = 6); -Asserttrue (Sub.maxsubarr (arr2) = = 14); theAsserttrue (Sub.maxsubarr (ARR3) = =-1); *Asserttrue (Sub.maxsubarr (ARR4) = = 2); $     }Panax Notoginseng}

The structure directory of the entire file is as follows:

Program Unit test methods:-Right-click Test Class->run as->junit tests

Operation Result: (errors:0,failure:0 If the test passes, if an error occurs, failure is the error that the unit test expects, stating that your code results do not meet your requirements, errors needs you to review the configuration of the system and check the code. is the manual input much faster? )

Description

Summary: The test class needs to inherit the Junit.framework.TestCase class, and the test method naming needs to follow the public void testxxxx format, using assertions to determine the correct row of the result.

Test scope must meet all of your needs

To test Testmaxsubarr as an example, the specific test examples and purposes are as follows:

Use Case number

Use case description

Input data Expected output data Pass/No Pass Evaluation
1 Basic functionality to detect the ability to implement the maximum array sum (general case) 1,2,3,-1 6 Pass Program implementation Basic functions
2 The largest subarray is in the middle of the array (general) 1,2,-4,8,4,-4,6,-2,1 14 Pass Program implementation Basic functions
3 Array elements are negative, and the first element is the smallest (special case) -4,-2,-1,-3 -1 Pass Program to achieve the summation of special cases
4 Multiple (>=2) sub-arrays with the same maximum value in the array (special case) -4,2,-4,2,-1 2 Pass The program can persist multiple identical sub-arrays

Now that we have the basic unit tests, of course it's very simple to use JUnit, and later I'll write some more specific introductions about JUnit.

Code: Https://coding.net/u/fench/p/java-junit/git

JUnit Test in Java

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.