如何捕获特定的空对象?
我有一个带4个参数的课程。也有一个测试,可将空对象放置。 是否可以捕获该零的特异性? 我的意思是有些测试将IlligalargumentException.class插入测试。因此,如果我尝试用尝试捕获块的整个Coctuctor Block捕获这个空对象,则此功能可行,但其他测试也会陷入困境,反之亦然。
class Quadrilateral extends Figure{
Quadrilateral(Point a, Point b, Point c, Point d){
}
}
@Test
void testConstructor() {
Figure q = null;
q = q(0, 0, 0, 1, 1, 1, 1, 0);
q = q(-2, 2, -3, 1, 0, 1, 0, 2);
}
@Test
void testConstructorNullACase() {
assertThrows(IllegalArgumentException.class, () -> q(null, new Point(-3, 1), new Point(0, 1), new Point(1, 9)));
}
I have a class with 4 parametres. Also have a test that puts null object.
Is it possible to catch specificaly this null?
I mean some tests put IlligalArgumentException.class inside tests . So if I try to catch this null object with try catch block for whole costructor block this one works but other tests crushes and vice versa.
class Quadrilateral extends Figure{
Quadrilateral(Point a, Point b, Point c, Point d){
}
}
@Test
void testConstructor() {
Figure q = null;
q = q(0, 0, 0, 1, 1, 1, 1, 0);
q = q(-2, 2, -3, 1, 0, 1, 0, 2);
}
@Test
void testConstructorNullACase() {
assertThrows(IllegalArgumentException.class, () -> q(null, new Point(-3, 1), new Point(0, 1), new Point(1, 9)));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用Lombok API。如果您用
@nonnull
注释一个参数,并且将其设置为null
它将抛出nullpointerexception
。LOMBOK代码:
Vanilla Java:
更多信息: lombok
You can use the Lombok API for this. If you annotate a parameter with
@NonNull
and it gets set tonull
it throws anNullPointerException
.Lombok code:
Vanilla Java:
More infos at: Lombok