软件构造Lab4中的3.4. Testing for Robustness and Correctness思考与实现:

博客介绍了Java代码的测试策略,采用等价类和边界值思想,针对实现CircularOrbit接口的三个类按Exception类型划分等价类并编写测试文件。还提及测试用例设计、运行结果与覆盖度报告,用SpotBugs工具发现错误并修改,强调RuntimeException与Exception处理应分开,否则有安全隐患。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

具体代码在:这里(求打星!!)
Testing strategy
使用等价类和边界值的测试思想。
在这里插入图片描述在这里插入图片描述

鉴于新增的情况主要出现在三个具体应用的实现CircularOrbit接口的类中,此处具体对这三个类的测试策略进行描述。
三个类中按3.1中定义的三种Exception的类型进行等价类划分,并对这些异常写了相应的三个.txt测试文件。通过读文件时捕获异常并对异常的类进行比对、对日志文件中记录的情况进行字符串比对等操作,确定原java文件是否正确。

测试用例设计
错误文档的构造:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述在这里插入图片描述

在这里插入图片描述在这里插入图片描述
在这里插入图片描述在这里插入图片描述在这里插入图片描述

@Test
	public void testWrongtxt()
	{
		CircularOrbit<CentralObject, PhysicalObject> cir = CircularOrbit.empty("Q3");
		try {
			cir.readfile("test AtomicStructureWrong.txt");
		} catch (ReadFileFailException e) {
			ReadFileFailException exp = new ReadFileFailException();
			assertEquals(exp.getClass(), e.getClass());
		}catch (Exception e) {
			System.out.println("The path is not valid, please re-enter it.");
		}
		cir = CircularOrbit.empty("Q3");
		try {
			cir.readfile("test AtomicStructureWrong2.txt");
		} catch (ReadFileFailException e) {
			ReadFileFailException exp = new ReadFileFailException();
			assertEquals(exp.getClass(), e.getClass());
		}catch (Exception e) {
			System.out.println("The path is not valid, please re-enter it.");
		}
		cir = CircularOrbit.empty("Q3");
		cir.islegal();
		try {
			cir.readfile("test AtomicStructureWrong3.txt");
		} catch (ReadFileFailException e) {
			ReadFileFailException exp = new ReadFileFailException();
			assertEquals(exp.getClass(), e.getClass());
		}catch (Exception e) {
			System.out.println("The path is not valid, please re-enter it.");
		}
	}
@Test
	public void testWrongtxt()
	{
		CircularOrbit< CentralObject, PhysicalObject> cir = CircularOrbit.empty("Q5");
		try {
			cir.readfile("test SocialNetworkCircleWrong.txt");
		} catch (ReadFileFailException e) {
			ReadFileFailException exp = new ReadFileFailException();
			assertEquals(exp.getClass(), e.getClass());
		}catch (Exception e) {
			System.out.println("The path is not valid, please re-enter it.");
		} 
		try {
			cir.islegal();
		} catch (Exception e) {
			// TODO: handle exception
		}
		cir = CircularOrbit.empty("Q5");
		try {
			cir.readfile("test SocialNetworkCircleWrong2.txt");
		} catch (ReadFileFailException e) {
			ReadFileFailException exp = new ReadFileFailException();
			assertEquals(exp.getClass(), e.getClass());
		}catch (Exception e) {
			System.out.println("The path is not valid, please re-enter it.");
		} 
		cir = CircularOrbit.empty("Q5");
		try {
			cir.readfile("test SocialNetworkCircleWrong3.txt");
		} catch (ReadFileFailException e) {
			ReadFileFailException exp = new ReadFileFailException();
			assertEquals(exp.getClass(), e.getClass());
		}catch (Exception e) {
			System.out.println("The path is not valid, please re-enter it.");
		} 
		cir = CircularOrbit.empty("Q5");
		try {
			cir.addcenterobject(null);
		} catch (Throwable e) {
			// TODO: handle exception
		}
		try {
			cir.deletecentralobj(null);
		} catch (Throwable e) {
			// TODO: handle exception
		}
		try {
			cir.deletecontactco(null, null);
		} catch (Throwable e) {
			// TODO: handle exception
		}
		try {
			cir.deleteorbitobj(null);
		} catch (Throwable e) {
			// TODO: handle exception
		}
		try {
			cir.deletecontactoo(null, null);
		} catch (Throwable e) {
			// TODO: handle exception
		}
		try {
			cir.addtoorbit(null, null);
		} catch (Throwable e) {
			// TODO: handle exception
		}
		try {
			cir.addcontactoo(null, null, null);
		} catch (Throwable e) {
			// TODO: handle exception
		}
		try {
			cir.addcontactco(null, null, null);
		} catch (Throwable e) {
			// TODO: handle exception
		}
	}
@Test
	public void testWrongtxt()
	{
		CircularOrbit< CentralObject, PhysicalObject> cir = CircularOrbit.empty("Q2");
		try {
			cir.readfile("test StellarSystemWrong.txt");
		} catch (ReadFileFailException e) {
			ReadFileFailException exp = new ReadFileFailException();
			assertEquals(exp.getClass(), e.getClass());
		}catch (Exception e) {
			System.out.println("The path is not valid, please re-enter it.");
		} 
		cir = CircularOrbit.empty("Q2");
		try {
			cir.readfile("test StellarSystemWrong2.txt");
		} catch (ReadFileFailException e) {
			ReadFileFailException exp = new ReadFileFailException();
			assertEquals(exp.getClass(), e.getClass());
		}catch (Exception e) {
			System.out.println("The path is not valid, please re-enter it.");
		} 
		cir = CircularOrbit.empty("Q2");
		try {
			cir.readfile("test StellarSystemWrong3.txt");
		} catch (ReadFileFailException e) {
			ReadFileFailException exp = new ReadFileFailException();
			assertEquals(exp.getClass(), e.getClass());
		}catch (Exception e) {
			System.out.println("The path is not valid, please re-enter it.");
		} 
	}

测试运行结果与EclEmma覆盖度报告
在这里插入图片描述在这里插入图片描述

SpotBugs tool
发现了哪些错误,每种错误代表什么不良的编程习惯
对代码修改,消除这些错误。
在这里插入图片描述

整体检测后发现这一个错误(出现在三个不同的应用中)
bug描述:
在这里插入图片描述

说明RuntimeException与Exception的处理最好要分开进行,否则会有很大的安全隐患,因为RuntimeException多是不可处理的,程序代码中出现的错误,而Exception是可以处理的,可预见的错误,二者不可同日而语。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值