Skip to content
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

Closed
3 tasks done
ridono opened this issue Oct 18, 2018 · 7 comments
Closed
3 tasks done

Comments

@ridono
Copy link

ridono commented Oct 18, 2018

Overview

Asserting that an int equals an Integer is not possible with assertEquals() due to the compiler not knowing if it should use assertEquals(int, int) or assertEquals(Object, Object).

This results in the compile error The method assertEquals(int, int) is ambiguous for the type Assertions like in the following example:

	@Test
	void test_parseYear() throws Exception {
		Integer parsedYear = SeparateDateParser.parseYear("2014");
		Assertions.assertEquals(2014, parsedYear); // does not compile
	}

Instead you either have to declare another variable:

	@Test
	void test_parseYear() throws Exception {
		Integer parsedYear = SeparateDateParser.parseYear("2014");
		Integer expectedYear = 2014;
		Assertions.assertEquals(expectedYear, parsedYear);
	}

or use a custom assertion method:

	void assertEquals(Number expected, Number actual) {
		Assertions.assertEquals(expected, actual);
	}

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

  • Support mixed boxed and unboxed primitives in assertEquals()
  • Support mixed boxed and unboxed primitives in assertNotEquals()
  • Document in Release Notes
@sbrannen
Copy link
Member

sbrannen commented Oct 18, 2018

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());
}

@sbrannen
Copy link
Member

sbrannen commented Oct 18, 2018

I agree that that is a bit of a nuisance.

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?

@sbrannen sbrannen added this to the 5.4 M1 milestone Oct 18, 2018
@sbrannen
Copy link
Member

Tentatively slated for 5.4 M1 for team discussion.

@sbrannen
Copy link
Member

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 master (which contains the changes in #1607).

@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 assertNotEquals(), we should do the same for assertEquals() (as raised in this GitHub issue) for consistency.

I am therefore removing the "team discussion" label.

@sbrannen sbrannen changed the title AssertEquals should provide assertion for mixed boxed and unboxed primitves Provide support for mixed boxed and unboxed primitves in assertEquals() & assertNotEquals() Oct 20, 2018
@sbrannen sbrannen changed the title Provide support for mixed boxed and unboxed primitves in assertEquals() & assertNotEquals() Support mixed boxed and unboxed primitves in assertEquals() & assertNotEquals() Oct 20, 2018
@sbrannen sbrannen self-assigned this Oct 20, 2018
@sbrannen
Copy link
Member

in progress

@sbrannen
Copy link
Member

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 static in Assertions does not remove the ambiguity for the compiler. 😞

@sbrannen sbrannen changed the title Support mixed boxed and unboxed primitves in assertEquals() & assertNotEquals() Support mixed boxed and unboxed primitives in assertEquals() & assertNotEquals() Oct 20, 2018
sbrannen added a commit that referenced this issue Oct 20, 2018
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
sbrannen added a commit that referenced this issue Oct 20, 2018
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
@sbrannen
Copy link
Member

This has been addressed in master in 0ec5a7b and 382e9ea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants