2

How do I enable assertions in IntelliJ? My code compiles but the assertions do not show up anywhere on the output.

For example:

public static void main(String[] args) { 
    int someInt = 5;
    assert someInt > 5; 
    assert someInt <= 5; 
}
Freiheit
  • 8,408
  • 6
  • 59
  • 101
anon
  • 33
  • 6
  • Can you please post an example of how you are using `assert` and where it is failing? https://stackoverflow.com/help/minimal-reproducible-example – Freiheit Aug 19 '21 at 12:51
  • one example: ` public static void main(String[] args) { int someInt = 5; assert someInt > 5; assert someInt <= 5; }` this results in an empty console, no exceptions thrown – anon Aug 22 '21 at 08:48
  • I ran your code in IntelliJ version 2020.3 and JDK 8. I do not see the assertions in the IntelliJ console either. – Freiheit Aug 23 '21 at 13:14

1 Answers1

6

Based on this answer for Eclipse you need to pass a JVM option -ea to enable assertions in any JVM. It's not unique to any specific IDE.

For IntelliJ it looks like this in the run config: IntelliJ 2020.3 Run Dialog to enter ea option

Note that you may have to select "Modify Options" to make the JVM options input box visible to enter the -ea flag.

Freiheit
  • 8,408
  • 6
  • 59
  • 101