Junit测试实例1
来源:互联网 发布:淘宝被买家投诉售假 编辑:程序博客网 时间:2024/05/16 20:29
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;
- }
- }
- 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测试
- 关闭Visual Studio 实时调试器
- 计算机视觉期刊
- poj 2075 Tangled in Cables
- Javascript中$符号的意思
- java技术之UrlClassLoader 实例 -- UrlClassLoader的作用
- Junit测试实例1
- 按日统计销售情况例子(每天的期末值是下一天的期初值)重要
- 卸载Intel C++ Compiler后Visual C++ 6.0不能工作了
- 引用、指针、抽象类的多态 in C++ and C#
- DSP CSL的使用(cache,timer)
- js 比较日期大小
- ListCtrl排序详解
- 如何获取图片的倒映及剪切、放缩图片?
- STL中accumulate函数的使用