如何在代码中为junit测试禁用Java断言 [英] How do I disable Java assertions for a junit test in the code

查看:82
本文介绍了如何在代码中为junit测试禁用Java断言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在代码中为junit测试禁用Java断言(不是junit assert)

How do I disable java assertions (not junit assert) for a junit test in the code

我已经编写了一个junit测试,但是当我运行它时,它不会按预期失败,因为启用了断言,而断言不在生产中.

Ive written a junit test but when I run it it doesnt fail as expected because assertions are enabled, whereas they are not in production.

有没有一种方法可以仅在代码中禁用断言,以使其在IDE中运行以及作为Maven的一部分构建时可以按预期工作

Is there a way to disable assertions just in the code so that it work as expected when run within IDE and when built as part of Maven

推荐答案

通常,您将带有Maven的surefire用于JUnit测试.添加-disableassertions或同义词-da作为参数应该可以:

Typically you use surefire with maven for JUnit tests. Adding -disableassertions or the synonym -da as an argument should work:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>your_version</version>
    <configuration>
        <argLine>-disableassertions</argLine>
    </configuration>
</plugin>

如果通过IDE(在maven之外)启动了不同的测试,则可能需要编辑一些启动配置并传递参数.但是,这取决于IDE.

If the tests are launched different through the IDE (that is outside of maven), you probably need to edit some launch configuration and pass the parameter. This is, however, IDE dependent.

这篇关于如何在代码中为junit测试禁用Java断言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆