Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit: Enable assertions in class under test

I've been bit a few times by Java assert statements that didn't fail in the JUnit test suite because assertions weren't enabled in JUnit's JVM instance. To be clear, these are "black box" assertions inside implementations (checking invariants, etc) not the assertions defined by the JUnit tests themselves. Of course, I'd like to catch any such assertion failures in the test suite.

The obvious solution is to be really careful to use -enableassertions whenever I run JUnit, but I'd prefer a more robust solution. One alternative is to add the following test to every test class:

  @Test(expected=AssertionError.class)   public void testAssertionsEnabled() {     assert(false);   } 

Is there a more automatic way to accomplish this? A system-wide configuration option to JUnit? A dynamic call I could put in the setUp() method?

like image 299
Chris Conway Avatar asked Nov 25 '09 16:11

Chris Conway


People also ask

Can assertion be enabled for classes in Java?

To configure assertion options one must use either the -ea or -da command line flags to enable or disable assertions with the command line tool: “java”. For example, “java -ea Assert” where Assert is a java class file. You may also specify a specific class or package as follows.

Can assertion be enabled for packages?

You could enable assertions for all packages, then disable them for some of the packages. Or otherwise- disable for all packages, then enable only for some of them.


2 Answers

In Eclipse you can go to WindowsPreferencesJavaJUnit, which has an option to add -ea everytime a new launch configuration is created. It adds the -ea option to the Debug Configuration as well.

The full text next to a check box is

Add '-ea' to VM arguments when creating a new JUnit launch configuration

like image 70
RAbraham Avatar answered Sep 21 '22 20:09

RAbraham


I propose three possible (simple?) fixes which work for me after a quick test (but you might need to check the side effects of using a static-initializer-block)

1.) Add a static-initializer block to those testcases which rely on assertions being enabled

import .... public class TestXX.... ...     static {         ClassLoader.getSystemClassLoader().setDefaultAssertionStatus(true);     }    ...    @Test(expected=AssertionError.class)    ... ... 

2.) Create a base-class which all of your test-classes extend which need assertions enabled

public class AssertionBaseTest {     static {         //static block gets inherited too         ClassLoader.getSystemClassLoader().setDefaultAssertionStatus(true);     } } 

3.) Create a test suite which runs all your test

import org.junit.runner.RunWith; import org.junit.runners.Suite;  @RunWith(Suite.class) @Suite.SuiteClasses({     //list of comma-separated classes     /*Foo.class,     Bar.class*/ }) public class AssertionTestSuite {     static {         //should run before the test classes are loaded         ClassLoader.getSystemClassLoader().setDefaultAssertionStatus(true);     }     public static void main(String args[]) {         org.junit.runner.JUnitCore.main("AssertionTestSuite");     } } 
like image 44
jitter Avatar answered Sep 20 '22 20:09

jitter