JUnit software testing

The directory structure is as follows

Calculator source code

 1 package com.lfr.core;
 2 
 3 public class Calculator
 4 {
 5     public int add(int number1,int number2)
 6     {
 7         return number1 + number2;    
 8     }
 9     
10     public int subtract(int number1,int number2)
11     {
12         return number1 - number2;
13     }
14     
15     public int multiply(int number1,int number2)
16     {
17         return number1 * number2;
18     }
19     
20     public int divide(int number1,int number2)
21     {
22         return number1 / number2;
23     }
24     
25     public double sub(double a,double b){
26         return a-b;
27     }
28 
29}
View Code

Test source code (the above should be capitalized, it is not very standardized here)

 1 package com.lfr.junit;
 2 
 3 import com.lfr.core.Calculator;
 4 import junit.framework.TestCase;
 5 
 6 public class test extends TestCase
 7 {
 8     private Calculator calculator;
 9     @Override
10     protected void setUp() throws Exception
11     {
12         calculator = new Calculator();
13     }
14     
15     @Override
16     protected void tearDown() throws Exception
17     {
18         
19     }
20     
21     public void testAdd()
22     {    
23         int value = calculator.add(1,2);
24         assertEquals(3,value);
25     }
26     
27     public void testMultiply()
28     {
29         int value = calculator.multiply(1, 2);        
30         assertEquals(2,value);
31     }
32     
33     public void testSubtract(){
34         int value=calculator.subtract(4, 2);
35         assertEquals(2, value);
36     }
37     
38     public void testDivide(){
39         int value=calculator.divide(6, 2);
40         assertEquals(3, value);
41     }
42     
43     public void testSub(){
44         double value=calculator.sub(3.0, 2.0);
45         assertEquals(1.0, value);
46     }
47 }
View Code

The important thing here is to import the Framework of Junit

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325027258&siteId=291194637
Recommended