19

I wanna do something like

java -enableassertions com.geeksanonymous.TestClass

How do I do this?

andandandand
  • 21,946
  • 60
  • 170
  • 271

8 Answers8

31

With a Maven project in netbeans 7.0, choose "File" -> "Project Properties ()".

In the window that appears, choose "Run", and add -enableassertions to the "VM Options" textbox.

MrDrews
  • 2,139
  • 2
  • 22
  • 22
5

For regular Apps and Swing Apps:

  1. Right click on the project in the Project Explorer
  2. Choose Properties (at the bottom of pop up menu)
  3. Choose Run (under Categories)
  4. Set the VM Options field to include -enableassertions
  5. Click [OK]

If you are working on a Web App you'll have to use a method that enables assertions on the server:

  1. Menu: Tools/Servers
  2. Select your server (on the left)
  3. Click on Platform table (middle right)
  4. Set the VM Options field to include -enableassertions
  5. Click [Close]
Peter Dobson
  • 311
  • 3
  • 4
5

I dont know about Netbeans, but you also can programmatically enable assertions (maybe that helps you as well).

public class WhenRunningTests() {

    static {
        ClassLoader.getSystemClassLoader().setDefaultAssertionStatus(true);
    }

    @Test(expected=AssertionError.class)
    public void assertionsShouldBeEnabled() {
        assert false;
    }
}
akuhn
  • 27,477
  • 2
  • 76
  • 91
  • That only set the assertion status to enabled for classes which are loaded after the statement in `static` is executed, see https://stackoverflow.com/a/44123936 for details. In order to be of help this answer would need to include a generic location where to invoke this statement. I didn't find one so far. – Kalle Richter May 23 '17 at 09:09
3

The easiest way is to use the Run properties. The property is labeled 'VM Options'.

This tutorial has more detailed info and screen shots from NetBeans 5.5. The dialog is very similar in the most recent release of NetBeans; 6.8, which is available today.

vkraemer
  • 9,864
  • 2
  • 30
  • 44
2

If your are programming a web application using Tomcat the set the -enableassertions in the server settings, not on the project settings.

In NetBeans 7.2.1 with Tomcat 6 or 7 go to: Tools (on the main menu) -> Servers -> Tomcat X.X -> Platform tab -> VM Options and add -enableassertions, then restart the server.

Kalle Richter
  • 8,008
  • 26
  • 77
  • 177
fishjd
  • 1,617
  • 1
  • 18
  • 31
2

Right click to your project and select Properties -> Run -> Add new configuration (select a name for the configuration, in my case I named it "assertion"). Then select VM options and write -enableassertions.

enter image description here

Kalle Richter
  • 8,008
  • 26
  • 77
  • 177
0

Yes, it's a bug in NetBeans that it does not enable assertions when running unit tests (https://netbeans.org/bugzilla/show_bug.cgi?id=139035). What Adrian suggests will work (although the test failed, yet assertions were enabled for the code that I was concerned with). Another way is to edit build-impl.xml and add in the macro definition for junit (just search for "junit").

0

The easiest way in latest version of Netbeans 8.0 will be... Go to RUN->Set Project Configuration->go for "customize"

then a window pops-up again click on "Customize" in front of VM Options n select "ea" in the options.