I’m trying to ensure that a parameter can’t be null by adding an assert statement at the top of the method.
When unit testing, I’m trying to declare that the AssertError is expected, but it still gets recognized as a failed test even though it’s behaviour is correct (AssertError is getting thrown).
class ExampleTest {
@Test(expected=AssertError.class)
public void testAssertFails() {
assert 0 == 1;
}
}
You probably need to enable assertions with the
-eaJVM argument, since they’re off by default. When the assertions are disabled, then theassertwon’t throw an exception if it fails.If you’re running this in Eclipse you can edit your Installed JRE in preferences to add this as an argument, or you add it to the run configuration for your tests.
Also, the exception thrown is
AssertionError, notAssertError.