How to use (primitive) auto-boxing / extension with Hamcrest?

I met https://code.google.com/p/hamcrest/issues/detail?id=130 to add sugar syntax for Hamcrest matches. But the idea was rejected by the developers of Hamcrest.

Any other smart ideas to make tests more readable, avoiding the need to type L for long ones?

@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 differences when comparing, for example. int and long using default Java laguage (==), standard junit assert (assertTrue) and hamcrest is () method. It seems that the odd hamcrest doest does not support long vs int matching / comparison, and the rest does not.

@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>
}
+4
source share
1

, , . , , Java .

L, , is. :

assertThat(longValue, greaterThan(1));
assertThat(longList, contains(1, 2, 3));

:

public static Matcher<Long> is(Integer value) {
    return org.hamcrest.core.Is.is(value.longValue());
}

, , int long, float double:

public static Matcher<Long> is(Float value) {
    return org.hamcrest.core.Is.is(value.longValue());
}

public static Matcher<Long> is(Double value) {
    return org.hamcrest.core.Is.is(value.longValue());
}

Java byte Integer *, byte short. , , int double?

public static Matcher<Double> is(Integer value) {
    return org.hamcrest.core.Is.is(value.doubleValue());
}

: (Integer)

, ! , Java . , .

, , , Hamcrest , . , , 1L 1.0 .

* byte int, Integer.

+2

All Articles