aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How to test programmatically wheather assertions have been enabled? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How to test programmatically wheather assertions have been enabled?" Watch "How to test programmatically wheather assertions have been enabled?" New topic
Author

How to test programmatically wheather assertions have been enabled?

Joey Sanchez
Ranch Hand

Joined: Jun 23, 2011
Posts: 86

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: 18896
    
  40

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: 86

Very clear!
Thank you very much
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to test programmatically wheather assertions have been enabled?