Compounding Calculator 4.0 in the second encounter with JUnit
Although the previous compound interest Calculator version has tried to use JUnit unit testing, it is not well-used because it has not been systematically studied, and the main problem is that there are too few scenarios to test, and not to achieve the comprehensiveness required for testing, Failed to embody the essence of the JUnit artifact, this version of the compounding calculator has been perfected in JUnit unit testing.
As shown, I put the test class unified in the newly created testing source file (test wrong, has been changed), in order to be separated from the program code, easy to operate.
Look carefully you will be able to find that the test directory under the package and program src directory corresponding function code package name consistent, this is to show that the test module for, practice reference from Horse soldier Teacher's teaching video, should also be considered a kind of operation Standard Bar ...
The JUnit tests for this program are mainly used in two ways:
- The test of logical calculation--used to detect the result of the program after the legal input, the logical calculation of the test program has achieved the expected effect.
The following is an example of a test for compounding end calculations.
The code is as follows:
1 @Test2 Public voidtestcalculate () {3 Booleanresult =false;4Calculateservice cs =NewCalculateservice ();5 DoubleTest = Cs.calculatebenjin (2000000,0.03,10);6 if(Test = = 1488187.82979345) result =true;7Assertequals (true, result);8}
The test results are as follows:
Other logical computing modules are tested similarly, and are not listed.
2. Test the operation scenario--for testing the possible actions of various users, such as input of various data, to detect whether the program responds to these actions in accordance with the expected effect. The following is an example of a test for the final amount of compound interest
Test code:
1 @Test2 Public voidTESTCALCULATEINPUT1 () {//Legal Input3Calculateservice cs =NewCalculateservice ();4 Booleanresult = Cs.calculshichanginput (200, 0.5, 100);5Assertequals (true, result);6 }7 @Test8 Public voidTestCalculateInput2 () {//Illegal operation: The final amount is negative9Calculateservice cs =NewCalculateservice ();Ten Booleanresult = Cs.calculshichanginput (-200, 0.5, 100); OneAssertequals (false, result); A } - @Test - Public voidTestCalculateInput3 () {//interest rate is negative theCalculateservice cs =NewCalculateservice (); - Booleanresult = Cs.calculshichanginput (200,-0.5, 100); -Assertequals (false, result); - } + @Test - Public voidTestCalculateInput4 () {//Illegal operation: Interest rate greater than 1 +Calculateservice cs =NewCalculateservice (); A Booleanresult = Cs.calculshichanginput (200, 2, 100); atAssertequals (false, result); - } - @Test - Public voidTestCalculateInput5 () {//Illegal Operation: The principal is negative -Calculateservice cs =NewCalculateservice (); - Booleanresult = Cs.calculshichanginput (200, 0.5,-100); inAssertequals (false, result); - } to @Test + Public voidTestCalculateInput6 () {//Illegal operation: Principal is greater than final amount -Calculateservice cs =NewCalculateservice (); the Booleanresult = Cs.calculshichanginput (200, 0.5, 300); *Assertequals (false, result); $}
3. The test results are as follows:
This test class is based on different scenarios to design different tests, the specific scenario please see the comments in the display code.
It is important to note that some method tests do not necessarily have the correct return value with true, such as the illegal input interception function in this program, should return false when the illegal input is detected is correct. As shown in the following:
JUnit is really a very useful thing, and very flexible, usage varies by program, also varies from person to person, I just just contact, know also limited, use a bit stiff but feel than the last time used to be handy, later still have to practice more.
GitHub Code Portal:
Please click here
Compounding Calculator 4.0 again with JUnit