assert vs. JUnit Assertions

JavaJunitAssertAssertions

Java Problem Overview


Today I saw a JUnit test case with a java assertion instead of the JUnit assertions—Are there significant advantages or disadvantages to prefer one over the other?

Java Solutions


Solution 1 - Java

In JUnit4 the exception (actually Error) thrown by a JUnit assert is the same as the error thrown by the java assert keyword (AssertionError), so it is exactly the same as assertTrue and other than the stack trace you couldn't tell the difference.

That being said, asserts have to run with a special flag in the JVM, causing many tests to appear to pass just because someone forgot to configure the system with that flag when the JUnit tests were run - not good.

In general, because of this, I would argue that using the JUnit assertTrue is the better practice, because it guarantees the test is run, ensures consistency (you sometimes use assertThat or other asserts that are not a java keyword) and if the behavior of JUnit asserts should change in the future (such as hooking into some kind of filter or other future JUnit feature) your code will be able to leverage that.

The real purpose of the assert keyword in java is to be able to turn it off without runtime penalty. That doesn't apply to unit tests.

Solution 2 - Java

I prefer JUnit assertions as they offer a richer API than the built-in assert statement and, more importantly do not need to be explicitly enabled unlike assert, which requires the -ea JVM argument.

Solution 3 - Java

When a test fails you get more infomation.

assertEquals(1, 2); results in java.lang.AssertionError: expected:<1> but was:<2>

vs

assert(1 == 2); results in java.lang.AssertionError

you can get even more info if you add the message argument to assertEquals

Solution 4 - Java

I'd say use JUnit asserts in test cases, and use java's assert in the code. In other words, real code shall never have JUnit dependencies, as obvious, and if it's a test, it should use the JUnit variations of it, never the assert.

Solution 5 - Java

I would say if you are using JUnit you should use the JUnit assertions. assertTrue() is basically the same as assert, Otherwise why even use JUnit?

Solution 6 - Java

This may not apply if you exclusively use stuff that's shiny and new, but assert was not introduced into Java until 1.4SE. Therefore, if you must work in an environment with older technology, you may lean towards JUnit for compatibility reasons.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRobertView Question on Stackoverflow
Solution 1 - JavaYishaiView Answer on Stackoverflow
Solution 2 - JavaAdamskiView Answer on Stackoverflow
Solution 3 - JavastarmerView Answer on Stackoverflow
Solution 4 - JavaBruno D. RodriguesView Answer on Stackoverflow
Solution 5 - JavaCheesePlsView Answer on Stackoverflow
Solution 6 - JavaDan CoatesView Answer on Stackoverflow