测试要求:
对我们和复利计算程序,写单元测试。
有哪些场景?
期待的返回值
写测试程序
运行测试
场景演示
1. 结果是否正确?
2. 数值为空是怎么办?
3.输入负数是否准确?
4.是否满足性能要求?
场景1. 结果是否正确?
功能 | 数据名称 | 具体数据 | 期待值 |
单利计算 | 本金,项目利率,年限,终值 | 100,0.05,1,105 | true |
-100,0.05,1,105 | false | ||
复利计算 | 本金,项目利率,年限,复利次数,终值 | 100,0.05,1,1,105 | true |
-100,0.05,1,1,105 | false | ||
本金估算 | 项目利率,年限,复利次数,终值,本金 | 0.05,1,1,105,100 | true |
0.05,1,1,-105,100 | false | ||
。。。 | |||
结果:
代码如下:
package com.Junit.test; import static org.junit.Assert.*; import interest.BestProject; import interest.CompoundInterrest; import interest.InterestTime; import interest.PeriodicIncome; import interest.Principal; import interest.Refund; import interest.SingleInterest; public class Test { @org.junit.Test public void testCompoundInterrest() { double f = new CompoundInterrest("0.05","100.0","1","1").Interrest(); assertEquals(105, (int)f); } @org.junit.Test public void testSingleInterest() { double f = new SingleInterest("0.05","100.0","1").Interest(); assertEquals(105, (int)f); } @org.junit.Test public void testInterestTime() { int t = new InterestTime("0.05","100.0","105.0","1").Interrest(); assertEquals(1, t); } @org.junit.Test public void testPeriodicIncome() { double f = new PeriodicIncome("0.01","100","1").Interrest(); assertEquals(101, (int)f); } @org.junit.Test public void testPrincipal () { double f = new Principal("0.05","105.0","1","1").Interrest(); assertEquals(100, (int)f); } @org.junit.Test public void testBestProject () { double f = new BestProject("200","100","1","1").Interrest(); assertEquals(1, (int)f); } @org.junit.Test public void testRefund () { double f = new Refund("0.87","1200","3").Interrest(); assertEquals(94, (int)f); } }
场景2. 数值为空是怎么办?
场景图如下:
数值为空时,场景图如下:
代码如下:
if(jt1.getText().trim().equals("") || jt2.getText().trim().equals("") ||jt3.getText().trim().equals("") ){ JOptionPane jo = new JOptionPane(); jo.showMessageDialog(null,"请输入数值!"); } else { System.out.print("22222222222"); interest (); }
本产品由本人与同伴一起完成!
心得感悟
在写程序的时候出现了许多错误,但都通过上网查找资料和与同伴商量解决了。同时在使用单元测试的时候也深深的感受到了单元测试检测程序的方便性,大大降低了程序员测试的工作量,提高了工作的效率!
详细代码:https://github.com/hanqilin/interest
时间: 2024-10-14 19:11:57