15

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

1
  • 1
    I always feel sad when I see questions like this. Commented Aug 18, 2019 at 13:08

4 Answers 4

14

Within Java (disabling assertions in single class)

To enable or disable assertion checking within Java use setClassAssertionStatus in the ClassLoader. For example:

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

Within Maven (disabling assertions for all classes)

Since version 2.3.1, Maven Surefire has a separate enableAssertions flag.

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

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:

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.

With JUnit 5 Assume

JUnit 5 assumptions are an extension of the JUnit 4 ones. Thus, for JUnit 5, the only change to the JUnit 4 code is the import, which now is from jupiter:

import static org.junit.jupiter.api.Assumptions.assumeTrue; 
Sign up to request clarification or add additional context in comments.

Comments

12

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.

2 Comments

I would disable them through class or package as described here: docs.oracle.com/javase/7/docs/technotes/guides/language/…
the question was if I could modify assertion behaviour within code, not within parameters passed to java. But Im assuming that is impossible and therefore you give the correct solution.
3

Look at -da and -ea parameters for java tool. So, in you case just use one of them (+ specify correspond package on your application) as you want to disable or enable assertions.

With no arguments, -enableassertions or -ea enables assertions.

  • With one argument ending in "...", the switch enables assertions in the specified package and any subpackages.
  • If the argument is "...", then the switch enables assertions in the unnamed package in the current working directory.
  • With one argument not ending in "...", the switch enables assertions in the specified class.

Comments

1

Note: Do you use such (java) assertion at all, if you disable assertions during tests? 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!

You can expect exception (AssertionError extends Error extends Throwable) in test. Use assertThrows in junit testing for example.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.