0

Is there a way to assert that a List contains only/exactly long[] array?

Code:

 // arrange
    long[] result = {1, 2, 3, 4, 5};

    // act
    List<Long> digitPowNumbers = SumDigPower.findDigitPowNumbers(1, 6);

    // assert
    assertThat(digitPowNumbers).containsExactly(result);
}

I'm getting Cannot resolve method containsExactly(long[]). How can I do this assertion? Is there a way without just typing 1, 2, 3, 4, 5 into containsExactly ?

doublemc
  • 3,021
  • 5
  • 34
  • 61

1 Answers1

2

containsExactly() expects an array of the same element type as your list, which is Long, not the primitive long. Change your array type and it should work:

Long[] result = {1L, 2L, 3L, 4L, 5L};
shmosel
  • 49,289
  • 6
  • 73
  • 138
  • Im getting incompatible types (required Long Found int) in the array. Do I have to add L to the integers for it to work? – doublemc Feb 13 '17 at 23:30
  • It's the only way? I have quite a lot of arrays and it's going to be very tiresome to add Ls everywhere – doublemc Feb 13 '17 at 23:33
  • 1
    @doublemc You can also write a custom matcher. – Tom Feb 13 '17 at 23:35
  • 1
    @doublemc If you're using Guava, you can do `List result = Longs.asList(1, 2, 3, 4, 5);` and compare them using `equals()`. With Apache Commons, you can do `Long[] result = Array.toObject(new long[] {1, 2, 3, 4, 5});`. Otherwise I don't have any simple workaround. – shmosel Feb 13 '17 at 23:43
  • With Java8 you can convert your `long` `array` into a `List` via `Arrays.stream(result).mapToObj(Long::valueOf).collect(Collectors.toList());` without using any extra library. – Florian Schaetz Feb 14 '17 at 07:43
  • 1
    You can likely use some regex to replace `number,` by `numberL,`. – Joel Costigliola Feb 15 '17 at 00:06