Junit测试实例1
来源:互联网 发布:淘宝添加客服怎么设置 编辑:程序博客网 时间:2024/05/17 06:45
Junit测试格言两则:
1)keep the bar green to keep the code clean!!!
2)单元测试不是证明您是对的,而是证明您没有错误!!!
被测试类代码
package com.zyg.test.junit3;
- /**
- * 数学计算
- *
- * @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;
- }
- }
测试类代码
- 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());
- }
- }
- Junit测试实例1
- Junit测试实例1
- junit测试简单实例
- junit测试入门实例
- Junit测试实例
- 第二天junit测试实例
- Android测试(四) Junit实例
- 软件测试技术JUnit和单元测试入门简介--JUnit实例
- junit 白盒测试简单实例
- Eclipse Android Junit Test 测试实例
- Android JUnit测试说明和实例演示
- JUnit测试框架的使用实例
- 使用junit+mockito进行mock测试实例
- android自动化测试1-Junit
- JUnit测试【1】(断言)
- JUnit测试总结(1)
- junit测试
- JUnit测试
- 关于GIS大学生涯规划的思考
- 神州数码最佳实践之一:向项目管理要效益
- ZJU_3048 Continuous Same Game
- 神州数码最佳实践之二:项目成功与盈利的关键因素——项目估算
- ==从零开始==
- Junit测试实例1
- 神州数码最佳实践之三:以项目为中心的结构设置
- MFC标准dll形式
- 神州数码最佳实践之四:项目的人员考核和激励
- 项目型组织的组织结构设置变迁史
- 纪念日
- mysql5.0安装配置
- UltraEdit-32 14.20.0.1035 官方简体中文版 + SN
- 道德底线(男女都该看)