简体   繁体   English

在Netbeans中启用断言

[英]Enabling assertions in Netbeans

I wanna do something like 我想做点什么

java -enableassertions com.geeksanonymous.TestClass

How do I do this? 我该怎么做呢?

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 -enableassertions to the "VM Options" textbox. 在出现的窗口中,选择“运行”,并将“ -enableassertions ”添加到“VM Options”文本框中。

For regular Apps and Swing Apps: 对于常规应用和Swing应用:

  1. Right click on the project in the Project Explorer 在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 将VM Options字段设置为包含-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: 如果您正在使用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 将VM Options字段设置为包含-enableassertions
  5. Click [Close] 点击[关闭]

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;
    }
}

If your are programming a web application using Tomcat the set the -enableassertions in the server settings, not on the project settings. 如果您正在使用Tomcat编写Web应用程序, -enableassertions在服务器设置中设置-enableassertions ,而不是在项目设置中。

In NetBeans 7.2.1 with Tomcat 6 or 7 go to: Tools (on the main menu) -> Servers -> Tomcat XX -> Platform tab -> VM Options and add -enableassertions , then restart the server. 在带有Tomcat 6或7的NetBeans 7.2.1中,转到: Tools (on the main menu) -> Servers -> Tomcat XX -> Platform tab -> VM Options并添加-enableassertions ,然后重新启动服务器。

The easiest way is to use the Run properties. 最简单的方法是使用Run属性。 The property is labeled 'VM Options'. 该属性标记为“VM选项”。

This tutorial has more detailed info and screen shots from NetBeans 5.5. 本教程提供了NetBeans 5.5中更详细的信息和屏幕截图。 The dialog is very similar in the most recent release of NetBeans; 在最新版本的NetBeans中,该对话框非常相似; 6.8, which is available today. 6.8,今天可用。

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"). 右键单击您的项目,然后选择Properties -> Run -> Add new configuration (选择Properties -> Run -> Add new configuration的名称,在我的情况下,我将其命名为“断言”)。 Then select VM options and write -enableassertions . 然后选择VM options并写入-enableassertions

在此输入图像描述

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 ). 是的,NetBeans中的一个错误是它在运行单元测试时不启用断言( 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). Adrian建议使用的是什么(尽管测试失败了,但是对我所关注的代码启用了断言)。 Another way is to edit build-impl.xml and add in the macro definition for junit (just search for "junit"). 另一种方法是编辑build-impl.xml并添加junit的宏定义(只搜索“junit”)。

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->设置项目配置 - >转到“自定义”

then a window pops-up again click on "Customize" in front of VM Options n select "ea" in the options. 然后再次弹出一个窗口,单击VM Options前面的“Customize”,在选项中选择“ea”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM