如何以编程方式测试是否启用断言?

时间:2022-03-01 23:01:40

One of the correct answers from OCP Java SE 6 Programmer Practice Exams is:

OCP Java SE 6程序员实践考试的正确答案之一是:

You can programmatically test wheather assertions have been enabled without throwing an AssertionError.

您可以在不抛出AssertionError的情况下,以编程方式测试是否启用了其他断言。

How can I do that?

我怎么做呢?

4 个解决方案

#1


25  

I use this

我用这个

boolean assertOn = false;
// *assigns* true if assertions are on.
assert assertOn = true; 

I am not sure this is the "official" way.

我不确定这是否是“官方”方式。

#2


19  

I guess you should use Class.desiredAssertionStatus()

我想你应该使用class。desiredassertionstatus ()

http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#desiredAssertionStatus()

http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html desiredAssertionStatus()

#3


14  

The Oracle Java Tutorial provides information about how to do it...

Oracle Java教程提供了如何做的信息……

http://docs.oracle.com/javase/7/docs/technotes/guides/language/assert.html

http://docs.oracle.com/javase/7/docs/technotes/guides/language/assert.html

An excerpt from the tutorial

本教程的摘录

7. Why not provide a construct to query the assert status of the containing class?

7所示。为什么不提供一个构造来查询包含类的断言状态?

Such a construct would encourage people to inline complex assertion code, which we view as a bad thing. Further, it is straightforward to query the assert status atop the current API, if you feel you must:

这样的构造将鼓励人们内联复杂的断言代码,我们认为这是一件坏事。此外,如果您认为必须:

boolean assertsEnabled = false;
assert assertsEnabled = true; // Intentional side-effect!!!
// Now assertsEnabled is set to the correct value

#4


0  

RuntimeMXBean mx = ManagementFactory.getRuntimeMXBean();
boolean assertionsEnabled = mx.getInputArguments().contains("-ea");

#1


25  

I use this

我用这个

boolean assertOn = false;
// *assigns* true if assertions are on.
assert assertOn = true; 

I am not sure this is the "official" way.

我不确定这是否是“官方”方式。

#2


19  

I guess you should use Class.desiredAssertionStatus()

我想你应该使用class。desiredassertionstatus ()

http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#desiredAssertionStatus()

http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html desiredAssertionStatus()

#3


14  

The Oracle Java Tutorial provides information about how to do it...

Oracle Java教程提供了如何做的信息……

http://docs.oracle.com/javase/7/docs/technotes/guides/language/assert.html

http://docs.oracle.com/javase/7/docs/technotes/guides/language/assert.html

An excerpt from the tutorial

本教程的摘录

7. Why not provide a construct to query the assert status of the containing class?

7所示。为什么不提供一个构造来查询包含类的断言状态?

Such a construct would encourage people to inline complex assertion code, which we view as a bad thing. Further, it is straightforward to query the assert status atop the current API, if you feel you must:

这样的构造将鼓励人们内联复杂的断言代码,我们认为这是一件坏事。此外,如果您认为必须:

boolean assertsEnabled = false;
assert assertsEnabled = true; // Intentional side-effect!!!
// Now assertsEnabled is set to the correct value

#4


0  

RuntimeMXBean mx = ManagementFactory.getRuntimeMXBean();
boolean assertionsEnabled = mx.getInputArguments().contains("-ea");