简体   繁体   English

如何在代码中禁用 junit 测试的 Java 断言

[英]How do I disable Java assertions for a junit test in the code

How do I disable java assertions (not junit assert) for a junit test in the code如何在代码中为 junit 测试禁用 java 断言(不是 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.我已经编写了一个 junit 测试,但是当我运行它时它并没有按预期失败,因为断言已启用,而它们不在生产中。

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有没有办法仅在代码中禁用断言,以便它在 IDE 中运行以及作为 Maven 的一部分构建时按预期工作

Typically you use surefire with maven for JUnit tests. 通常,您可以使用surefire和maven进行JUnit测试。 Adding -disableassertions or the synonym -da as an argument should work: 添加-disableassertions或同义词-da作为参数应该有效:

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

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. 如果测试是通过IDE(在maven之外)启动的,则可能需要编辑一些启动配置并传递参数。 This is, however, IDE dependent. 但是,这取决于IDE。

Within Java (disabling assertions in single class) 在Java中(禁用单个类中的断言)

To enable or disable assertion checking within Java use setClassAssertionStatus in the ClassLoader. 要在Java中启用或禁用断言检查,请使用ClassLoader中的setClassAssertionStatus For example: 例如:

Foo.class.getClassLoader().setClassAssertionStatus(Foo.class.getName(), false);

Within Maven (disabling assertions for all classes) 在Maven中(禁用所有类的断言)

Since version 2.3.1, Maven Surefire has a separate enableAssertions flag. 从版本2.3.1开始,Maven Surefire有一个单独的enableAssertions标志。

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

With JUnit 4 Assume (for single test case) 使用JUnit 4 Assume(针对单个测试用例

Ideally, your tests should pass regardless of assertions are enabled or disabled. 理想情况下,无论启用还是禁用断言,您的测试都应该通过。 If one of the tests depends on assertions being disabled, the intended JUnit mechanism is to use an assumption : 如果其中一个测试依赖于被禁用的断言,那么预期的JUnit机制将使用一个假设

import static org.junit.Assume.assumeTrue;

@Test
public foo onlyWithoutAssertions() {
    assumeTrue(assertionsDisabled());
    // your tricky test comes here, and is only executed in 
    // environments with assertion checking disabled.
}

public static boolean assertionsDisabled() {
    return !Foo.class.desiredAssertionStatus();
}

Note: I typically use this option the other way around: To check that an assert works as expected, in rare cases I have a test that assumes that assertion checking is enabled . 注意:我通常以相反的方式使用此选项:要检查assert按预期工作,在极少数情况下,我有一个假定断言检查已启用的测试

With JUnit 5 Assume 使用JUnit 5 Assume

JUnit 5 assumptions are an extension of the JUnit 4 ones. JUnit 5假设是JUnit 4的扩展。 Thus, for JUnit 5, the only change to the JUnit 4 code is the import, which now is from jupiter: 因此,对于JUnit 5,对JUnit 4代码的唯一更改是导入,现在来自jupiter:

import static org.junit.jupiter.api.Assumptions.assumeTrue;

Look at -da and -ea parameters for java tool. 查看java工具的-da-ea 参数 So, in you case just use one of them (+ specify correspond package on your application) as you want to disable or enable assertions. 因此,在您的情况下,只需使用其中一个(+在应用程序中指定对应的包),因为您要禁用或启用断言。

Note: Do you use such (java) assertion at all, if you disable assertions during tests?注意:如果您在测试期间禁用断言,您是否完全使用此类 (java) 断言? If no, just remove given assertion!如果没有,只需删除给定的断言! It is just boilerplate.它只是样板文件。

If you use it (in some UI tests, acceptance tests or in performance tests) and thus you are expecting that given asserts will catch some regressions and they will be not triggered during normal release pipeline, it is good thing to check if someone has not removed such assertion without any knowledge!如果您使用它(在某些 UI 测试、验收测试或性能测试中)并且因此您期望给定的断言会捕获一些回归并且它们不会在正常发布管道中被触发,那么检查是否有人没有在不知情的情况下删除了这样的断言!

You can expect exception (AssertionError extends Error extends Throwable) in test.您可以在测试中预期异常(AssertionError extends Error extends Throwable)。 Use assertThrows in junit testing for example.例如,在 junit 测试中使用assertThrows

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

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