I came across https://code.google.com/p/hamcrest/issues/detail?id=130 to add some sugar syntax for Hamcrest matchers. But the idea was rejected by the Hamcrest developers.
Any other smart ideas to make tests better readable by avoiding having to type L behind longs?
@Test
public void test1() {
int actual = 1;
assertThat(actual, is(1));
}
@Test
public void test2() {
long actual = 1L;
assertThat(actual, is(1)); // fails as expected is <1> but result was <1L>
// assertThat(actual, is(1L)); off course works..
}
@Test
public void test3() {
Long actual = new Long(1);
assertThat(actual, is(1)); // fails as expected is <1> but result was <1L>
}
UPDATE
See also below the differences when comparing e.g. int and long using default Java laguage (==), standard junit assert (assertTrue) and the hamcrest is() method. It seems odd hamcrest doest not supporting matching/comparing long vs int and the rest is.
@Test
public void test2() {
long actual = 1L;
int expected = 1;
assertTrue(expected == actual); // std java succeeds
assertEquals(expected, actual); // std junit succeeds
assertThat(actual, is(expected)); // hamcrest fails: Expected: is <1> but: was <1L>
}