• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to test programmatically wheather assertions have been enabled?

 
Joey Sanchez
Ranch Hand
Posts: 88
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21419
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Joey Sanchez
Ranch Hand
Posts: 88
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very clear!
Thank you very much
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic