如何为JUNIT编写Java程序的异常测试?
package BasicTesting;
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Factorial_of_number {
public static int factorial(int n)//factorial method
{
if (n == 0) {
return 1;
} else if (n < 1) {
return -1;
} else {
return n * factorial(n - 1);
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter number to factorize ");
int n = input.nextInt();
System.out.println(factorial(n));
}
}
在这里,当我们给出负数为输入时,它们的处理方式不佳。实际上,该程序应返回我们找不到负数的阶乘。通过使用此程序,我们如何编写JUNIT测试案例以检查该程序是否给出了负数的正确输出?他们没有检查负数,因此在我们测试异常时,系统必须失败,对吗?我也附上了我的Junit Test案例。
package BasicTesting;
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class FactorialTest {
public FactorialTest(int input, int expected) {
super();
this.input = input;
this.expected = expected;
}
Factorial_of_number ob = new Factorial_of_number();
private int input;
private int expected;
@Parameters
public static Iterable<Object[]> testConditions() {
return Arrays.asList(new Object[][]{{0, 1}, {1, 1},
{5, 120}, {-3, -1}, {10, 3628800}, {12, 479001600}});
}
@Test
public void factorial_test() {
assertEquals(expected, Factorial_of_number.factorial(input));
}
}
如何检查该例外?我也应该更改我的源程序吗?还请提供编辑的代码。
package BasicTesting;
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Factorial_of_number {
public static int factorial(int n)//factorial method
{
if (n == 0) {
return 1;
} else if (n < 1) {
return -1;
} else {
return n * factorial(n - 1);
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter number to factorize ");
int n = input.nextInt();
System.out.println(factorial(n));
}
}
Here, when we give negative numbers as input, they are not handled well. Actually, the program should return that we can't find factorial for negative numbers. By using this program, how can we write JUnit test cases to check whether the program give correct output for negative numbers? They are not checking negative numbers, so the system must fail while we test for exceptions, right? I am attaching my JUnit test case also.
package BasicTesting;
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class FactorialTest {
public FactorialTest(int input, int expected) {
super();
this.input = input;
this.expected = expected;
}
Factorial_of_number ob = new Factorial_of_number();
private int input;
private int expected;
@Parameters
public static Iterable<Object[]> testConditions() {
return Arrays.asList(new Object[][]{{0, 1}, {1, 1},
{5, 120}, {-3, -1}, {10, 3628800}, {12, 479001600}});
}
@Test
public void factorial_test() {
assertEquals(expected, Factorial_of_number.factorial(input));
}
}
How can I check for that exception? Should I change my source program too? Kindly provide the edited code also.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,当您当前返回-1时,您的阶乘方法将有例外,以便您的方法看起来像这样 -
然后您可以使用这样的测试用例 -
First of all, you will have throw exception from your factorial method for negative values as you are returning -1 currently so your method will look like this -
Then you can have your test cases like this -
首先,您必须在测试方法中声明何时以及哪种类型的异常。
然后,对于Junit4,有3种主张例外的主要方法。
1)非常原始和非时尚的方式:
这是基本方法,但仍然可以使用。
2)快速易于阅读:
最好如果要识别失败的测试,但不需要细节。
3)最复杂的,最适合详细测试
如果要测试特定方案,例如异常消息或原因。
可以有一种方法可以带有不同类型或消息的多个异常 - 那么这是最佳选择。
在测试注释之前初始化:
测试本身:
我想这远远超出了您的问题,但将来可能会方便。
First of all, you have to declare when and what type of exception is thrown in your testing method.
Then, for JUnit4 there are 3 main ways to assert an exception is thrown.
1) very primitive and non-elegant way:
It's basic way but still usable.
2) fast and easy to read:
Best if you want to identify failed test, but don't need details.
3) most complex, best for detailed testing
Use this if you want to test specific scenario, like exception message or cause.
There can be a method which throws multiple exceptions with different types or messages - then this is the best choice.
Initialize before test annotation:
Test itself:
I guess this is far beyond your question, but it can be handy in future.