java 在 Netbeans 中启用断言

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1880587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 18:20:53  来源:igfitidea点击:

Enabling assertions in Netbeans

javanetbeansassertions

提问by andandandand

I wanna do something like

我想做类似的事情

java -enableassertions com.geeksanonymous.TestClass

How do I do this?

我该怎么做呢?

采纳答案by vkraemer

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

最简单的方法是使用 Run 属性。该属性标记为“VM 选项”。

This tutorialhas 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.

本教程包含来自 NetBeans 5.5 的更详细信息和屏幕截图。该对话框在最新版本的 NetBeans 中非常相似;6.8,今天可用。

回答by MrDrews

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

使用netbeans 7.0 中的Maven 项目,选择“文件”->“项目属性()”。

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

在出现的窗口中,选择“运行”,然后添加-enableassertions到“VM 选项”文本框。

回答by akuhn

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

我不了解 Netbeans,但您也可以以编程方式启用断言(也许对您也有帮助)。

public class WhenRunningTests() {

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

    @Test(expected=AssertionError.class)
    public void assertionsShouldBeEnabled() {
        assert false;
    }
}

回答by Peter Dobson

For regular Apps and Swing Apps:

对于常规应用程序和 Swing 应用程序:

  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]
  1. 在 Project Explorer 中右键单击该项目
  2. 选择属性(在弹出菜单底部)
  3. 选择运行(在类别下)
  4. 将 VM Options 字段设置为包含 -enableassertions
  5. 点击【确定】

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

如果您正在开发 Web 应用程序,则必须使用在服务器上启用断言的方法:

  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]
  1. 菜单:工具/服务器
  2. 选择您的服务器(在左侧)
  3. 单击平台表(右中)
  4. 将 VM Options 字段设置为包含 -enableassertions
  5. 点击【关闭】

回答by fishjd

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

如果您正在使用 Tomcat 编写 Web 应用程序-enableassertions,请在服务器设置中设置,而不是在项目设置中。

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

在带有 Tomcat 6 或 7 的 NetBeans 7.2.1 中,转到: Tools (on the main menu) -> Servers -> Tomcat X.X -> Platform tab -> VM Options并添加-enableassertions,然后重新启动服务器。

回答by Taimoor Shehzad

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 optionsand write -enableassertions.

右键单击您的项目并选择Properties -> Run -> Add new configuration(为配置选择一个名称,在我的例子中我将其命名为“断言”)。然后选择VM options并写入-enableassertions

enter image description here

在此处输入图片说明

回答by Nathan Fiedler

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").

是的,这是 NetBeans 中的一个错误,它在运行单元测试时不启用断言 ( https://netbeans.org/bugzilla/show_bug.cgi?id=139035)。Adrian 的建议会起作用(尽管测试失败了,但对我关心的代码启用了断言)。另一种方法是编辑 build-impl.xml 并添加 junit 的宏定义(只需搜索“junit”)。

回答by Rahul Golani

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

最新版本的 Netbeans 8.0 中最简单的方法是...转到 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.

然后再次弹出一个窗口,单击 VM 选项前面的“自定义”,然后在选项中选择“ea”。