关于Junit中Assert已经过时
在junit4.12之后,Assert就过时了,提供了TestCase来取代;
同样在TestCase中原本比较常见的一些方法也已经取消了,例如:assertNotEquals、assertThat、assertArraysEquals。
自己写了一个MathUtil方法,代码很简单,就不贴了:
package a_junit;
import org.junit.Test;
import junit.framework.Assert;
import junit.framework.TestCase;
/**
* 关于Junit的使用:
* 可以使用选中方法然后run as 1 Junit Test
* 也可以使用在online中查看缩略图,对缩略图中的方法进行run as
*
* 满足Junit Test的条件,必须是public void的(不能有返回值,必须是public的)
* 而且方法中没有参数(模拟main函数的入口)
* 可以抛出异常!
*
* @author mzy
*/
public class TestMathUtil {
@Test
public void testAdd() {
int a = 0;
int b = 5;
System.out.println(MathUtil.add(a, b));
}
@Test
public void testDiv() {
int a = 5;
int b = 3;
System.out.println(MathUtil.div(a, b));
}
@Test
public void testAddIsRight() {
/*
* 手动抛出错误!
*/
if(MathUtil.add(5, 10) != 15) {
throw new RuntimeException("错误答案!");
}
}
@Test
public void testDivIsRight() {
/*
* 使用Junit中专业的判定结果的方法:
* Assert 断言类
* 在junit4.12中,Assert过时,被TestCase取代
* assertNotEquals、assertThat、assertArraysEquals 已经取消
* 取消
*
* assertTrue
* assertFalse
* assertNotSame
* assertSame
* assertEquals
*
* assertNull
* assertNotNull
*/
// Assert.assertEquals(15, MathUtil.div(45, 3)); // 判断两个值是否相等
// Assert.assertNotSame(expected, actual);
TestCase.assertEquals(15, (int)MathUtil.div(45, 3)); // 使用equals进行判断(对象的话,可重写equals方法进行对象匹配)
TestCase.assertSame(15, MathUtil.add(10, 5)); // 使用 == 进行判断
TestCase.assertNotSame(15, MathUtil.add(10, 8));
}
}
关于Junit中Assert已经过时的更多相关文章
- JUnit中Assert简单介绍
junit中的assert方法全部放在Assert类中,总结一下junit类中assert方法的分类.1.assertTrue/False([String message,]boolean condi ...
- Junit中Assert.assertEquals()和Assert.assertSame方法有什么异同
1)提供的接口数量不完全相同.assertEquals支持boolean,long,int等等java primitiveType变量.assertSame只支持Object. 2)比较的逻辑不同,结 ...
- junit中的assert方法总结
junit中的assert方法全部放在Assert类中,总结一下junit类中assert方法的分类.1.assertTrue/False([String message,]boolean condi ...
- <p>在静态类junit.framework.Assert或者静态类org.junit.Assert中存在下面几个方法</p>
在静态类junit.framework.Assert或者静态类org.junit.Assert中存在下面几个方法 1.assertEquals()方法,用来查看对象中存的值是否是期待的值,与字符串比較 ...
- try....fail....catch...Assert 模式的测试, fail是Junit中的功能
try { // 反射读取properties文件 new BufferedReader(new FileReader(myConfigPath[4])); //上面没有抛出异常就是执行fail, / ...
- JUnit中测试异常抛出的方法
最近在做TWU关于TDD的作业,对JUnit中测试异常抛出的方法进行了一些学习和思考. 在进行单元测试的时候有的时候需要测试某一方法是否抛出了正确的异常.例如,我有一个方法,里面对一个List进行读取 ...
- junit断言和junit注释assert
JUnit - 使用断言 断言 所有的断言都包含在 Assert 类中 public class Assert extends java.lang.Object 这个类提供了很多有用的断言方法来编写测 ...
- 关于spring中Assert的应用(方法入参检测工具类)
关于spring中Assert的应用(方法入参检测工具类) Web 应用在接受表单提交的数据后都需要对其进行合法性检查,如果表单数据不合法,请求将被驳回.类似的,当我们在编写类的方法时,也常常需要对方 ...
- Junit中常用的注解说明
Java注解((Annotation)的使用方法是@注解名 ,能通过简单的词语来实现一些功能.在junit中常用的注解有@Test.@Ignore.@BeforeClass.@AfterClass.@ ...
随机推荐
- Leetcode3.无重复字符的最长子串——简洁易懂
> 简洁易懂讲清原理,讲不清你来打我~ 输入字符串,找到无重复.最长.子串,输出长度 排序用法
sort() 方法用于对数组的元素进行排序,并返回数组.默认排序顺序是根据字符串Unicode码点. 语法:array.sort(fun):参数fun可选.规定排序顺序.必须是函数.注:如果调用该方法 ...
- Linux下Nginx基础应用
Nginx简介: Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器.其将源代码以类BSD许可证的形式发布,因 ...
- zabbix latest.php SQL注入漏洞(CVE-2016-10134)
Zabbix 2.2.14之前的版本和3.0.4之前的3.0版本 latest.php页面提取cookie中的zbx_sessionid的后16位 246c58ba963457ef http://19 ...
- vsftpd安装配置
vsftpd安装配置 vsftpd测试服务器: 192.168.1.191 1.安装: yum provides */vsftpd yum install vsftpd -y 2.匿名用户最基本配置( ...