Java IllegalArgumentException.class 在 JUnit 中符合预期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19217588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IllegalArgumentException.class as expected in JUnit
提问by danksim
Let's say I have this simple method I want to unit test:
假设我有这个简单的方法要进行单元测试:
public class Add {
public static int addTheFollowingPositiveNumberToOne(int a) {
if(a <= 0) {
throw new IllegalArgumentException();
}
int b = 1 + a;
return b;
}
}
I want to make sure IllegalArgumentException is thrown if the parameter is less than or equal to 0.
如果参数小于或等于 0,我想确保抛出 IllegalArgumentException。
The JUnit code would look something like this:
JUnit 代码看起来像这样:
@Test (expected = IllegalArgumentException.class)
public void testIllegalArgumentException() {
int a = -1;
Add.addTheFollowingPositiveNumberToOne(a);
}
Why do I have to add .class at the end?
为什么我必须在最后添加 .class?
采纳答案by Jayamohan
If you see the API document of expected() parameter, it takes parameter of type Class
. That's why you are passing IllegalArgumentException
class type as IllegalArgumentException.class
.
如果你看到expected() 参数的API 文档,它需要类型的参数Class
。这就是为什么您将IllegalArgumentException
类类型作为IllegalArgumentException.class
.
回答by SudoRahul
When you write .class
after a class name, it references the Classobject that represents the given class.
当您.class
在类名之后写入时,它引用代表给定类的Class对象。
Also, another important thing to note is that
此外,另一个需要注意的重要事项是
You use the ClassName.class
when an instance of that class is not available. And you use classObject.getClass()
when an instance of the class available.
ClassName.class
当该类的实例不可用时,您可以使用。并且您classObject.getClass()
在类的实例可用时使用。
If you look at the docsof expected()
of the @Test
annotation,
如果你看一下文档中expected()
的的@Test
注解,
public abstract java.lang.Class expected
预期的公共抽象 java.lang.Class
Optionally specify expected, a Throwable, to cause a test method to succeed iff an exception of the specified class is thrown by the method.
可选地指定预期,一个 Throwable,以导致测试方法成功,如果该方法抛出指定类的异常。
expected
takes the parameter of type class
(which extends Throwable
) and thus you give .class
after the IllegalArgumentException
.
expected
采用类型class
(扩展Throwable
)的参数,因此您.class
在IllegalArgumentException
.
IllegalArgumentException
- extends throwable
.class
- gives the class object which is expected.
IllegalArgumentException
- extends throwable
.class
- 给出预期的类对象。
回答by user2339071
Actually, IllegalArgumentException
is a class that is thrown to indicate that a method has been passed an illegal or inappropriate argument.This is actually a class in java that extends RuntimeException
. This is the inheritance hierarchy.
实际上,IllegalArgumentException
是一个被抛出的类,表明一个方法已经传递了一个非法或不适当的参数。这实际上是 java 中的一个类,它扩展了RuntimeException
. 这是继承层次结构。
java.lang.Object
--java.lang.Throwable
--java.lang.Exception
--java.lang.RuntimeException
--java.lang.IllegalArgumentException
As for why you need to specify the @Test (expected = IllegalArgumentException.class)
, here is a simple explanation:
至于为什么需要指定@Test (expected = IllegalArgumentException.class)
,这里简单解释一下:
The @Test
annotation supports two optional parameters. The first, expected, declares that a test method should throw an exception. If it doesn't throw an exception or if it throws a different exception than the one declared, the test fails. For example, the following test succeeds:
该@Test
注解支持两个可选参数。第一个是预期的,声明一个测试方法应该抛出一个异常。如果它不抛出异常,或者抛出的异常与声明的异常不同,则测试失败。例如,以下测试成功:
@Test(expected=IndexOutOfBoundsException.class) public void outOfBounds() {
new ArrayList<Object>().get(1);
}
The second optional parameter, timeout, causes a test to fail if it takes longer than a specified amount of clock time (measured in milliseconds). The following test fails:
在第二个可选参数,超时,导致测试,如果它需要比时钟特定时间量(以毫秒为单位)长失败。以下测试失败:
@Test(timeout=100) public void infinity() {
while(true);
}
For reference check:
供参考检查:
Hope it helps. :)
希望能帮助到你。:)