两种测试异常处理
第一种
@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方法