junit 测试全过程

1 创建计算和字符串连接类

 

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.junit;

/**
 *
 * @author Huo
 */
public class Calculator {

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

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

 

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.junit;

/**
 *
 * @author Huo
 */
public class LinkeString {

    public String getlinkeString(String str1,String str2){
        String str="";
        str=str1+str2;
        return str;
    }
}

 

2 创建相应的测试类

 

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.junit;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author Huo
 */
public class CalculatorTest {
    /**
     * Test of add method, of class Calculator.
     */
    @Test
    public void testAdd() {
        System.out.println("start add...");
        int a = 10;
        int b = 20;
        Calculator instance = new Calculator();
        int expResult = 30;
        int result = instance.add(a, b);
        assertEquals(expResult, result);
    }

    /**
     * Test of minus method, of class Calculator.
     */
    @Test
    public void testMinus() {
        System.out.println("minus");
        int a = 30;
        int b = 20;
        Calculator instance = new Calculator();
        int expResult = 10;
        int result = instance.minus(a, b);
        assertEquals(expResult, result);
    }

}

 

 

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.junit;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author Huo
 */
public class LinkeStringTest {

    /**
     * Test of getlinkeString method, of class LinkeString.
     */
    @Test
    public void testGetlinkeString() {
        System.out.println("getlinkeString");
        String str1 = "hello";
        String str2 = "world";
        LinkeString instance = new LinkeString();
        String expResult = "helloworld";
        String result = instance.getlinkeString(str1, str2);
        assertEquals(expResult, result);
    }

}

 

 

3 建立suite 类,目的是把所有的测试用例都运行一遍

 

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.junit;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

/**
 *
 * @author Huo
 */

@RunWith(Suite.class)
@Suite.SuiteClasses({CalculatorTest.class,LinkeStringTest.class})

public class TestAll {}

 

 

4 总结

 

1 首先通过类的单元测试。

2 把所有的方法集合到一起进行测试。

 

以上称为单元自动化测试

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值