0

I am currently using FEST or AssertJ for assertion. and I run into a knot that I want to assert the flowing array:

[1,2,2,2,2,2,2]

So how do I write the assertion like

assertThat(arr).contains(1,atIndex(0)).containsTheOthers(2)

I don't see containsOthers in FEST or I miss something equal? I am a bit surprise FEST or AssertJ can't assert a range of index start from some designated index, since them emphasize on fluent concise assertion code. or is there good alternative?

As far I have to separate it into two asserts and manually fetch out first element to check and the n fetch out the others to check,totally three lines. That's a mess.

assertThat(arr[0]).contains(1,atIndex(0));
Arrays.copyOfRange(arr,1,arr.length);
assertThat(arr).containsOnly(2);
Hari Menon
  • 33,649
  • 14
  • 85
  • 108
WeiChing 林煒清
  • 4,452
  • 3
  • 30
  • 65

2 Answers2

4

Using AssertJ, I would write:

assertThat(arr).containsExactly(1,2,2,2,2,2,2);

But note that it will also check the elements order.

FYI, AssertJ also provide these "contains" assertions:

  • containsSequence
  • containsSubSequence
  • containsOnlyOnce

Have a look at the int array assertions javadoc

Joel Costigliola
  • 6,308
  • 27
  • 35
-1

Why complicate things? The simplest way to write this would be:

assertEquals(new int[]{1,2,2,2,2,2,2}, arr);

Otherwise, if there can be any number of 2s, your second solution to split it into two assertions seems sensible.

Joe
  • 29,416
  • 12
  • 68
  • 88