Skip to content
Advertisement

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

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.

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

Advertisement

Answer

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>

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.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement