junit基本用法
来源:互联网 发布:第三方数据监测平台 编辑:程序博客网 时间:2024/06/05 03:02
很久之前接触了一点junit,但一直没用过,最近代码量大了,于是回想起junit的好处。
junit共就6种注释,其中用得最多的就是@Test注释
使用junit前需要导包,现在用到的版本是junit-4.10.jar,大家可以去百度上下载相关jar包,也可以只用jdk里的junit包。
接下来创一个类
package yc;import org.junit.Test;public class math {public int add(int a,int b){return a+b;}public int jian(int a,int b){return a-b;}public int chen(int a,int b){return a*b;}public double chu(int a,int b){return a/b;}}
再写一段测试代码
/* @After
public void add(){
System.out.println(m.add(1, 2)); //3
}
@Test
public void jian(){
System.out.println(m.jian(2, 1));//1
}
@Ignore
public void chen(){
System.out.println(m.chen(2, 3));//6
}
@Before
public void chu(){
System.out.println(m.chu(4, 2));//2
}*/
运行结果:
2
1
3
也可以创建一个类来继承TestCase类,继承之后可以直接调用很多方法。如:
assertSame() 和 assertNotSame() 方法测试两个对象引用指向完全相同的对象。
assertNull() 和 assertNotNull() 方法测试一个变量是否为空或不为空(null)。
assertTrue() 和 assertFalse() 方法测试if条件或变量是 true 还是 false。
assertArrayEquals() 将比较两个数组,如果它们相等,则该方法将继续进行不会发出错误。否则失败将显示在JUnit窗口和中止测试。
代码实现:
//assertEquals(1, 2);
//assertFalse("错了", false);
//assertSame(m, m1);
//assertNull(m2);
注意:其中assertArrayEquals方法需要先手动导入import static org.junit.Assert.*;
还有一点需要注意,被测试的方法必须以“test”开头,例如“testDemo”;
- Junit测试基本用法
- JUnit 单元测试基本用法
- JUnit基本用法
- junit基本用法
- JUnit 4的基本用法
- Junit的基本用法 更多选项
- JUnit学习笔记(一):基本用法
- JUnit 用法
- junit用法
- Junit基本教程
- Junit基本教程
- Junit基本教程
- JUNIT基本教程
- JUNIT基本教程
- JUnit基本使用
- junit基本介绍
- junit的基本使用
- JUnit注解基本解释
- 马云砸4亿?贝索斯为智能音箱砸下10亿
- Google Data Binding Library 谷歌官方数据绑定库(二) 自定义绑定类名
- 中科慧眼获得近亿元融资 百度阿波罗基金领投
- JAVA中的反射机制
- spring学习总结(二)
- junit基本用法
- 在线流程图工具
- XML——小结一
- pytorch finetune模型
- 排序算法之快速排序--Java语言
- 查看作业执行时间
- activiti生成的表与service的描述
- java字符串的替换replace、replaceAll、replaceFirst的区别详解
- 依赖