测试异常处理
2021/12/8 23:47:19
本文主要是介绍测试异常处理,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
两种测试异常处理
第一种
@Test(expected = IllegalArgumentException.class)
第二种
@Rule
被测试的类
public class FizzBuzz { /** * Write a progarm that prints the numbers from 1 to 100. * Multiples of Three print "Fizz" instead of the number. * Multiples of Five print "Buzz" instead of the number. * Multiples of Three To Five print "Buzz" instead of the number. */ //constant public static String Three_Multiples = "Fizz"; public static String Five_Multiples = "Buzz"; public static String Three_And_Five_Multiples = "FizzBuzz"; public static String No_1_TO_100 = "Invalid data"; public static String getAnswerFor(int number){ isOvernumber(number); return Judge_Three_And_Five_Multiples(number); } private static void isOvernumber(int number){ if (Judge_No_1_To_100(number)){ throw new IllegalArgumentException(No_1_TO_100); } } public static String Judge_Three_And_Five_Multiples(int number) { if (Judge_Three_To_Five_Multiples(number)) { return Three_And_Five_Multiples; } if (Judge_Three_Multiples(number)) { return Three_Multiples; } if (Judge_Five_Multiples(number)) { return Five_Multiples; } return String.valueOf(number); } //check if it is a multiple of three private static boolean Judge_Three_Multiples(int number) { return number % 3 == 0; } //check if it is a multiple of five private static boolean Judge_Five_Multiples(int number) { return number % 5 == 0; } //check if it is a multiple of three and five private static boolean Judge_Three_To_Five_Multiples(int number) { return Judge_Three_Multiples(number) && Judge_Five_Multiples(number); } //check if it is the numbers from 1 to 100 private static boolean Judge_No_1_To_100(int number) { return number > 100 || number < 1; } }
@Test(expected = IllegalArgumentException.class)
import org.junit.Assert; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; public class FizzBuzzTest { private Integer Test_Small_1_Number = -1; private Integer Test_1_Number = 1; private Integer Test_2_Number = 2; private Integer Test_3_Number = 3; private Integer Test_5_Number = 5; private Integer Test_15_Number = 15; private Integer Test_101_Number = 101; /** * 测试大于100的数字 101 * 断言期望值与实际值是否相等 */ @Test(expected = IllegalArgumentException.class) public void FizzBuzz_Big_100() { //assert that the number is 101 and whether the expected value "Invalid data" is reached FizzBuzz.getAnswerFor(Test_Small_1_Number); }
FizzBuzz.getAnswerFor():测试的信息
由于是静态方法,所以直接 类名.方法名 调用 FizzBuzz中写好的getAnswerFor方法
由于是静态方法,所以直接 类名.方法名 调用 FizzBuzz中写好的getAnswerFor方法
@Rule
import org.junit.Assert; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; public class FizzBuzzTest { private Integer Test_Small_1_Number = -1; private Integer Test_1_Number = 1; private Integer Test_2_Number = 2; private Integer Test_3_Number = 3; private Integer Test_5_Number = 5; private Integer Test_15_Number = 15; private Integer Test_101_Number = 101; @Rule public ExpectedException thrown = ExpectedException.none(); /** * 测试循环小于于1的数字 -1 * 断言期望值:Invalid data与实际值是否相等 */ @Test public void FizzBuzz_Small_1() { //assert that the number is -1 and whether the expected value "Invalid data" is reached thrown.expect(IllegalArgumentException.class); thrown.expectMessage(FizzBuzz.No_1_TO_100); FizzBuzz.getAnswerFor(Test_Small_1_Number); }
expect():表明测试会抛出一个异常
expectMessage():抛出异常信息
FizzBuzz.getAnswerFor():测试的信息
由于是静态方法,所以直接 类名.方法名 调用 FizzBuzz中写好的getAnswerFor方法
这篇关于测试异常处理的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-11有哪些好用的家政团队管理工具?
- 2025-01-11营销人必看的GTM五个指标
- 2025-01-11办公软件在直播电商前期筹划中的应用与推荐
- 2025-01-11提升组织效率:上级管理者如何优化跨部门任务分配
- 2025-01-11酒店精细化运营背后的协同工具支持
- 2025-01-11跨境电商选品全攻略:工具使用、市场数据与选品策略
- 2025-01-11数据驱动酒店管理:在线工具的核心价值解析
- 2025-01-11cursor试用出现:Too many free trial accounts used on this machine 的解决方法
- 2025-01-11百万架构师第十四课:源码分析:Spring 源码分析:深入分析IOC那些鲜为人知的细节|JavaGuide
- 2025-01-11不得不了解的高效AI办公工具API