Junitテスト例1


[size=large]
Junit :
1)keep the bar green to keep the code clean!!!
2) , !!!
  
 
package com.zyg.test.junit3;
 
package com.zyg.test.junit3; 
import junit.framework.Assert; 
import junit.framework.TestCase; 

/** 
 * junit3.8 Test  
 * @author zyg 
 */ 
public class CalculatorTest extends TestCase{ 
    /** 
     * junit3.8  
     * 1.public  
     * 2.void 
     * 3.  
     * 4. test  
     */ 
    public void testAdd(){ 
        Calculator cal = new Calculator(); 
        int result  = cal.add(1, 2); 
         
        //   
        Assert.assertEquals(3,result); 
    } 
     
    public void testMinus(){ 
        Calculator cal = new Calculator(); 
        int result = cal.minus(1, 2); 
        Assert.assertEquals(-1, result); 
    } 
     
    public void testMultiply(){ 
        Calculator cal = new Calculator(); 
        int result = cal.multiply(2, 3); 
         
        //   
        Assert.assertEquals(6, result); 
    } 
     
    public void testDivide(){ 
        Calculator cal = new Calculator(); 
        int result = 0; 
        try { 
            result = cal.divide(6, 4); 
        } catch (Exception e) { 
            e.printStackTrace(); 
            Assert.fail(); 
        } 
         
        //   
        Assert.assertEquals(1,result); 
    } 
     
    public void testDivide2(){ 
        Throwable tx = null; 
        try{ 
            Calculator cal = new Calculator(); 
            cal.divide(4, 0); 
             
            Assert.fail(); 
        }catch(Exception ex){ 
            tx = ex; 
        } 
        System.out.println(tx.getMessage()); 
        Assert.assertNotNull(tx); 
        Assert.assertEquals(Exception.class, tx.getClass()); 
        Assert.assertEquals(" !!",tx.getMessage()); 
    } 
} 
/** 
 *   
 *  
 * @author zyg 
 *  
 */ 
public class Calculator { 

    public int add(int a, int b) { 
        return a + b; 
    } 

    public int minus(int a, int b) { 
        return a - b; 
    } 

    public int multiply(int a, int b) { 
        return a * b; 
    } 

    public int divide(int a, int b) throws Exception{ 
        if(0 == b){ 
            throw new Exception(" !!"); 
        } 
        return a / b; 
    } 
} 
[/size]