定义计算器的类
加法和减法
调用加法 1+2
减法
单元测试
创建单元测试类
我们要测试方法Add,所以定义的方法名为testAdd
在上面加上@Test报错了。因为工程里面并没有导入Junit的依赖
需要导入Junit的依赖环境
点击红色的小灯泡,选择第一个 添加Junit到classPath
点ok
调用计算器的方法。测试加法
在这里手动加一个异常。3是不能除以0的
再运行测试就会报错了。报了个数学异常
我们使用Junit最终看的是红色还是绿色。绿色是测试成功,红色是测试失败。
这里把异常再去掉
但是下面的加好改成减号
再次执行也是绿的。虽然执行成功了 但是结果 并不是我想要的 我要是加法。
一般不会在单独测试里面做输出。一般会做断言的操作。
参数1是期望值,我期望这个结果是3
运行后,断言失败了
再测试减法,期望值是-1.和运行结果result对比
绿色就表示通过了 不需要输出结果。