Assertion Error issue

183 Views Asked by At

I have posted this elsewhere, no solution yet, so posting it here as well. The below mentioned code is not throwing an assertion error as I expected it to since num is less than 5. Hope someone can advise. Thank you.

public class Wrong {  
public static void main(String[] args) {      
    Wrong wrong = new Wrong();            
    wrong.methodE(3);                 
    }     
    //AssertionError  
    void methodE(int num)  
    {  
        assert(num>5);  
    }  
}  
2

There are 2 best solutions below

0
On

I guess you forgot to enable assertions.

Run the jvm with the -ea argument.

java -ea ...

you should also consider to provide an assertion error message, e.g.

assert num > 5 : "arg num must be greater than 5";
1
On

If you are using Eclipse go to Run--> Run Configuration --> VM Argument ---> Type -ea.