Search code examples
javascjpassertionocpjp

How to programmatically test if assertions are enabled?


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

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

How can I do that?


Solution

  • I use this

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

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