Java assert gives strange results

what should the following java code do?

public class foo{
    public static void main(String[] args){
        boolean mybool=false;
        assert (mybool==true);
    }
}

Should this give an assertion error? And if not, then why? (I get no errors!)

+3
source share
4 answers

It must be chosen AssertionErors.

You need to enable claims if you are using Eclipse. By default, they are disabled.

To do this, add -ea to the JVM arguments.

0
source

When starting the program, you need to enable the statements in the Java virtual machine by adding the '-e' command line:

java -ea -jar myprogram.jar
+4
source

Java . , .

. Apache Commons Lang ( Validator), Spring ( Assert) JUnit4 ( Assert) , VM. Java5, , java assert, , .

+1

, .

( ), , mybool false.

if statements are enabled (jvm -ea argument), the statement will be executed and the side effect of mybool will be set to true.

You can use this to force enable or disable claims. For example, I have a Test in my TestSuites that fails if statements are not included to ensure that statements are always included when tests are run.

0
source

Source: https://habr.com/ru/post/1712879/


All Articles