Why is there no Assertions.assertEquals(boolean, boolean) method? #3647
Replies: 4 comments 2 replies
-
The Assertions API is designed to test expectations. This implies knowing what the expected value will be. So in case of a boolean, either true or false. Though you could accurately express the expectation that both services return then same boolean value by using Note: Not part of the JUnit team. And this API originally comes from JUnit 4, so finding the motivation of the original authors for using |
Beta Was this translation helpful? Give feedback.
-
Using Hopefully nobody writes I could create a PR to add another overloaded |
Beta Was this translation helpful? Give feedback.
-
There's no concrete need for such an assertion method. Primitive For example, the following test ... @Test
void assertBooleansAreEqual() {
boolean a = true;
boolean b = false;
assertEquals(a, b);
} ... fails with:
And that's by design. In light of that, I'm closing this discussion. |
Beta Was this translation helpful? Give feedback.
-
As I mentioned previously, we've never had a "concrete reason" to introduce In other words, the auto-boxing from If that is truly problematic for you, please open an issue to request new assertion methods; however, please read the following first.
We have those in order to support combinations of primitives and their wrappers and to ensure that compilers know which method to choose. Related Commits/Issues
No. See the linked issues above. In addition, as @mpkorstanje pointed out, I originally copied (almost) all assertions from JUnit 4 when we introduced the And please note that
Yes, of course. But... by my calculations we would need 12 new I'm not saying we couldn't do that. Rather, I'm just saying I'm not sure if it's warranted. In any case, like I said above, if you feel strongly about this, feel to create an issue, and we'll discuss it within the team. Cheers, Sam |
Beta Was this translation helpful? Give feedback.
-
If my counting is correct, then there are currently 93 overloaded "assertEquals" methods in the
org.junit.jupiter.api.Assertions
class (link to Javadoc). Despite this impressive API, there seems to be no simple assertEquals(boolean, boolean) method to compare two primitive boolean values in JUnit 5.I am aware of the
assertTrue(boolean)
andassertFalse(boolean)
methods. However, these methods imply that at the time of writing a test I already know whether or not a primitive boolean value should be true or false. This may often be the case, but not always. Every now and then it happens that I get two primitive boolean values programmatically at runtime and I want to make sure they are equal (= either both true or both false).Of course I can write my own little helper method and use some if-else as work-around:
value1 ? assertTrue(value2) : assertFalse(value2)
. But this is ugly and adds unnecessary complexity on each code base where there is such a need.Another work-around is to use the
assertEquals(Object, Object)
method and rely on auto-boxing from primitiveboolean
values toBoolean
objects. While this technically works, I would argue that this is also ugly and (depending on the configuration) may result in warnings from the compiler and/or static code analysis tools.I understand that API design is hard and that it is sometimes not possible to support all use-cases, somewhere a line has to be drawn. But given there are already 93 assertEquals() methods, including for all other primitive types (byte, char, float, double, int, short, long), the lack of a simple assertEquals(boolean, boolean) method looks to me like a gap and inconsistency in the API. It should be easy to fix this without adding a lot of complexity or breaking the backwards compatibility of JUnit, so I wonder if there are any reasons why this has not already been done long ago?
Beta Was this translation helpful? Give feedback.
All reactions