How to enable assert
Hello,
I have a project targeting 1.4.1, I checked the Project Properties box
"Enable 'assert' keyword and then restarted IntelliJ.
I then wrote a test program (see below) that has the following line in it:
assert(false) : "This should fail";
I ran this via IntelliJ both in 'release' and in 'debug' mode. I was
expecting to see a message in the IntelliJ output window telling me that the
assetion had failed. I did not see any such message in either release or
debug mode.
Is there something else that I need to do to enable assert other than simply
checking the check box and restarting IntelliJ?
Thanks,
Ted Hill
public class AssertionTestFrame extends JPanel
{
public AssertionTestFrame()
{
System.out.println("AssertionTestFrame.AssertionTestFrame");
assert(false) : "This should fail";
}
public static void main(String[ ] args)
{
JFrame frame = new JFrame("AssertionTestFrame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane( ).add(new AssertionTestFrame( ),
BorderLayout.CENTER);
frame.setSize(400, 200);
frame.setVisible(true);
}
}
Please sign in to leave a comment.
You need to use the vm parameter -ea (or -enableassertions)
Add it to the VM Parameters box of the run/debug section of the project
properties. This will cause an exception to be thrown when the assertion
fails.
N.
Ted Hill wrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
simply
Did you add the -Dea (and -Desa id you want) to the command line of the run
entry? The running jvm has assertions disabled by default.
I know this is a very old post, but I came across it, so maybe others will too.
I used the Run Configurations to add the "-ea" to my VM options, but to even see VM options I had to click "Modify Options" and then check "Add VM Options".
@ksnortum-This was very helpful for me. Thank you so much for mentioning it.