天天看點

junit(七)

package com.ygl;
public class Calculator {
    public  int add(int a,int b){
     try {
   Thread.sleep(500);
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
     return a+b;
    }
    
   public int divide(int a,int b) throws Exception{
   try{
    return a/b;
   }catch(Exception e){
    throw new Exception("除數不能為零");
    
   }
   }
 }//**************************************
package com.ygl;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
 import java.util.Collection;import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.junit.runners.Parameterized;
 import org.junit.runners.Parameterized.Parameters;@RunWith(Parameterized.class)
 public class ParametersTest {
     private int expected;
     private int input1;
     private int input2;
     private Calculator cal;
     @SuppressWarnings("rawtypes")
  @Parameters
     public static Collection  prepareData(){
   Object[][] object={{3,1,2},{-4,-1,-3},{5,2,3}};
      
      return Arrays.asList(object);
      
     }
     public ParametersTest(int expected ,int input1 ,int input2){
      this.expected=expected;
      this.input1=input1;
      this.input2=input2;
     }
     
     
     @Before
     public void setup() {
     cal=new Calculator();
  }
     
     @Test
     public void testAdd(){
      assertEquals(this.expected,cal.add(input1, input2));
     }
     
 }