| Author |
How to test programmatically wheather assertions have been enabled?
|
Joey Sanchez
Ranch Hand
Joined: Jun 23, 2011
Posts: 70
|
|
One of the answers of Practice Exams (OCP Java SE 6) questions is:
*You can programmatically test wheather assertions have been enabled without throwing an AssertionError. - Correct answer
and my question is How can we do that?
We can set Assertion Status
or
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16686
|
|
Joey Sanchez wrote:One of the answers of Practice Exams (OCP Java SE 6) questions is:
*You can programmatically test wheather assertions have been enabled without throwing an AssertionError. - Correct answer
and my question is How can we do that?
We can set Assertion Status
or
The Oracle Java Tutorial actually provides an example on how to do it...
http://docs.oracle.com/javase/1.4.2/docs/guide/lang/assert.html
And here is the excerpt from the tutorial....
7. Why not provide a construct to query the assert status of the containing class?
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
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Joey Sanchez
Ranch Hand
Joined: Jun 23, 2011
Posts: 70
|
|
Very clear!
Thank you very much
|
 |
 |
|
|
subject: How to test programmatically wheather assertions have been enabled?
|
|
|