Java Junit test and java Junit Test

Source: Internet
Author: User

Java Junit test and java Junit Test
Junit Test Question 1 in java: when to use assertTrue and assertFalse statements in JUnit

AssertTrue (boolean condition );

Condition: if the condition result is true, the test is passed.

AssertFalse (boolean condition );

Condition: if the condition result is false, the test is passed.


Question 2: java uses Junit to test instances


Java how to use JUnit for unit testing-Fench-blog


What is unit test?

Baidu encyclopedia explains this: unit testing (Module Testing) is a short piece of code written by developers to check whether a very small and clear function of the tested code is correct. Generally, a unit test is used to determine the behavior of a specific function under a specific condition (or scenario. For example, you may put a large value into an ordered list, and then confirm that the value appears at the end of the list. Alternatively, you may remove characters that match a certain pattern from the string and confirm that the string does not contain these characters.

To put it simply, unit testing is to test the smallest functional module in your program. It may be a function in C and a method or class in java.

The purpose is to improve the quality of the Code.

What is junit?

JUnit is a unit test framework in Java. It was established by Kent Beck and Erich Gamma and gradually became the most successful xUnit family from Kent Beck's sUnit. JUnit has its own JUnit extension ecosystem. Most Java development environments have integrated JUnit as a unit test tool.

That is to say, junit is a unit test framework written by others. Using this framework can greatly shorten your testing time and accuracy (I still remember the small program written in C language when I first came to my freshman year, every time the test is restarted, it is very painful to compile-input-stop-compile. It is much better to use the unit test framework of junit today ).


Note: eclipse already comes with JUnit. Currently common versions of JUnit are divided into 3.X( manually added) and 4.X( annotation supported)

In this version, eclipse comes with JUnit3.X. This document uses JUnit3.x as an example for testing.



The following example shows how to calculate the sum of the largest sub-array and how to get started with unit test.


1 package edu. sau. maximun; 2 3 public class SubArray {4 // calculate the maximum 5 public int max (int x, int y) of the two numbers {6 if (x> y) 7 return x; 8 else 9 return y; 10} 11 12 // sum of the largest sub-array 13 public int maxSubArr (int [] arr) {14 int sum = arr [0]; // The maximum subarray and 15 int sub_sum = arr [0]; // The center value 16 17 for (int I = 1; I <arr. length; I ++) {18 sub_sum = max (arr [I], sub_sum + arr [I]); 19 sum = max (sub_sum, sum ); 20} 21 return sum; 22} 23}

The above code implements the "maximum sub-array sum" algorithm. There are two methods: max and maxSubArr. Next we will start the test (re-create a new package and name it test. All tests will be completed in this new package)


1 package edu. sau. maximun. test; 2 3 import edu. sau. maximun. subArray; 4 import junit. framework. testCase; 5 6 public class TestMaximun extends TestCase {7 8 // test method max 9 public void testMax () {10 int x = 1; // Test Data 11 int y = 2; 12 int z =-1; 13 SubArray sub = new SubArray (); 14 int result1 = sub. max (x, y); 15 int result2 = sub. max (y, x); 16 int result3 = sub. max (z, x); 17 assertTrue (result1 = y); 18 assertTrue (result2 = y); 19 assertTrue (result3 = 1 ); 20} 21 22 // method of maxSubArr Test 23 public void testMaxSubArr () {24 25 int arr1 [] = {1, 2, 3,-1 }; // Test Case 26 int arr2 [] = {,-, 4,-,-}; 27 int arr3 [] = {-4,-2, -1,-3}; 28 int arr4 [] = {-,-,-1}; 29 SubArray sub = new SubArray (); 30 31 32 assertTrue (sub. maxSubArr (arr1) = 6); 33 assertTrue (sub. maxSubArr (arr2) = 14); 34 assertTrue (sub. maxSubArr (arr3) =-1); 35 assertTrue (sub. maxSubArr (arr4) = 2); 36} 37}


The structure directory of the entire file is as follows:



Program unit test method:-right-click the test class-> Run as-> JUnit test

Running result: (Errors: 0, Failure: 0 indicates that the test is successful. If an error occurs, Failure is the expected error in the unit test. It indicates that your code results do not meet your requirements, errors requires you to view the system configuration and check the code. Is manual input much faster ?)






Summary: The test class must inherit the junit. framework. TestCase class. The test method name must be in the public void TestXXXX format and the result is determined by assertions.


The test scope must meet all your needs.

Take testMaxSubArr as an example. The test example and purpose are as follows:

Case No.

Case Description

Input data Expected output data Pass/fail Rating
1 Measure the test taker's knowledge about whether the maximum array sum can be achieved (generally) 1, 2, 3,-1 6 Pass Program Implementation basic functions
2 The maximum sub-array is in the middle of the array (generally) 1, 2,-, 4 ,-,- 14 Pass Program Implementation basic functions
3 Array elements are negative, and the first element is the smallest (in special cases) -4,-2,-1,-3 -1 Pass Program to achieve the sum of special circumstances
4 Multiple subarrays with the same maximum value (> = 2) exist in the array (in special cases) -4,2,-4,2,-1 2 Pass The program can retain multiple identical subarrays.

Now, we will have basic unit tests. Of course, this is just a very simple use of junit. Later I will write some more details about junit.











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: 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.