-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support mixed boxed and unboxed primitives in assertEquals() & assertNotEquals() #1638
Comments
I agree that that is a bit of a nuisance. Note that the following is a workaround I often use: @Test
void test_parseYear() throws Exception {
Integer parsedYear = Integer.valueOf(2014);
Assertions.assertEquals(2014, parsedYear.intValue());
} |
Coming back to that... I suppose this nuisance exists not only for numbers but rather for all primitive types when asserting equality between a primitive and its wrapper type. So perhaps we should investigate a solution for all such cases. @junit-team/junit-lambda, thoughts? |
Tentatively slated for 5.4 M1 for team discussion. |
Actually, now that I've thought about it... Along the same lines, the changes in #1607 are breaking changes. Specifically, the following compiles against Jupiter 5.3.1 but does not compile against @Test
void parseYear() {
Integer parsedYear = Integer.valueOf("2014");
assertNotEquals(2014, parsedYear);
} Thus, in order to fix the breaking changes introduced in #1607, we will have to introduce methods that allow the compiler to properly pick the correct assertion method for a combination of boxed and unboxed values. And... since we have to do that for I am therefore removing the "team discussion" label. |
in progress |
Analysis Declaring methods like the one proposed in this issue's description only works if the methods are declared locally as in the following example. class MixedBoxedAndUnboxedPrimitivesTests {
void assertEquals(Number expected, Number actual) {
Assertions.assertEquals(expected, actual);
}
void assertEquals(Character expected, Character actual) {
Assertions.assertEquals(expected, actual);
}
void assertEquals(Boolean expected, Boolean actual) {
Assertions.assertEquals(expected, actual);
}
@Test
void bytes() {
assertEquals((byte) 42, Byte.valueOf("42"));
}
@Test
void shorts() {
assertEquals((short) 42, Short.valueOf("42"));
}
@Test
void integers() {
assertEquals(42, Integer.valueOf("42"));
}
@Test
void longs() {
assertEquals(42L, Long.valueOf("42"));
}
@Test
void floats() {
assertEquals(42.0f, Float.valueOf("42.0"));
}
@Test
void doubles() {
assertEquals(42.0d, Double.valueOf("42.0"));
}
@Test
void booleans() {
assertEquals(false, Boolean.valueOf("false"));
}
@Test
void chars() {
assertEquals('a', Character.valueOf('a'));
}
} Declaring those same methods as |
Prior to this commit, the following resulting in a compiler error due to ambiguity. assertEquals(42, Integer.valueOf("42")); The same holds true for all primitive types other than `boolean`. This commit addresses this shortcoming by introducing assertEquals() variants that support mixed boxed and unboxed primitive values. Issue: #1638
Prior to this commit, the following resulted in a compiler error due to ambiguity. assertNotEquals(42, Integer.valueOf("99")); The same holds true for all primitive types other than `boolean`. This commit addresses this shortcoming by introducing assertNotEquals() variants that support mixed boxed and unboxed primitive values. Issues: #1607, #1638
Overview
Asserting that an
int
equals anInteger
is not possible withassertEquals()
due to the compiler not knowing if it should useassertEquals(int, int)
orassertEquals(Object, Object)
.This results in the compile error
The method assertEquals(int, int) is ambiguous for the type Assertions
like in the following example:Instead you either have to declare another variable:
or use a custom assertion method:
We have already encapsulated this method in a utility class to reuse it in our projects, but as this is a quite common case it would be nice if the framework could provide an
assertEquals()
method like the one above.Related Issues
Deliverables
assertEquals()
assertNotEquals()
The text was updated successfully, but these errors were encountered: