2017-12-27 18 views
0

AbstractIterableAssert#containsOnlyの違いが何であるかは言う: AssertJ:containsOnlyとcontainsExactlyInAnyOrder

が実際のグループは、任意の順序で、唯一与えられた値と他には何が含まれていることを確認します。

AbstractIterableAssert#containsExactlyInAnyOrderは言う:

は、実際のグループは、任意の順序に正確に与えられた値と他には何も、が含まれていることを確認します。

説明はほとんど同じに見えますので、のみと正確間の実際の違いは何ですか?

答えて

2

は、実際の違いは問題で予想と実際のコレクション/リストは重複にが含まれている専用の場合:

  • containsOnlyは常にある敏感で複製:それが失敗したことがない場合に予想される/実際のセット値は一致します

  • containsExactlyInAnyOrderは常に重複しますsensitive:それは期待/実際の要素数が異なる場合は失敗し

場合、それらはすべて失敗し、けれども:ユニーク値が実際のコレクションに欠けている

  • 少なくとも一つを期待

  • すべてユニーク実際の値からの値がアサートされている

例を参照してください:

private List<String> withDuplicates; 
private List<String> noDuplicates; 

@Before 
public void setUp() throws Exception { 
    withDuplicates = asList("Entryway", "Underhalls", "The Gauntlet", "Underhalls", "Entryway"); 
    noDuplicates = asList("Entryway", "Underhalls", "The Gauntlet"); 
} 

@Test 
public void exactMatches_SUCCESS() throws Exception { 
    // successes because these 4 cases are exact matches (bored cases) 
    assertThat(withDuplicates).containsOnly("Entryway", "The Gauntlet", "Underhalls", "Entryway", "Underhalls"); // 1 
    assertThat(withDuplicates).containsExactlyInAnyOrder("Entryway", "The Gauntlet", "Underhalls", "Entryway", "Underhalls"); // 2 

    assertThat(noDuplicates).containsOnly("Entryway", "The Gauntlet", "Underhalls"); // 3 
    assertThat(noDuplicates).containsExactlyInAnyOrder("Entryway", "The Gauntlet", "Underhalls"); // 4 
} 

@Test 
public void duplicatesAreIgnored_SUCCESS() throws Exception { 
    // successes because actual withDuplicates contains only 3 UNIQUE values 
    assertThat(withDuplicates).containsOnly("Entryway", "The Gauntlet", "Underhalls"); // 5 

    // successes because actual noDuplicates contains ANY of 5 expected values 
    assertThat(noDuplicates).containsOnly("Entryway", "The Gauntlet", "Underhalls", "Entryway", "Underhalls"); // 6 
} 

@Test 
public void duplicatesCauseFailure_FAIL() throws Exception { 
    SoftAssertions.assertSoftly(softly -> { 
     // fails because ["Underhalls", "Entryway"] are UNEXPECTED in actual withDuplicates collection 
     softly.assertThat(withDuplicates).containsExactlyInAnyOrder("Entryway", "The Gauntlet", "Underhalls"); // 7 

     // fails because ["Entryway", "Underhalls"] are MISSING in actual noDuplicates collection 
     softly.assertThat(noDuplicates).containsExactlyInAnyOrder("Entryway", "The Gauntlet", "Underhalls", "Entryway", "Underhalls"); // 8 
    }); 
} 
+1

私は、 'containsOnly' javadocのそれを行うためのもう一つの理由はそれを明確にしようとしていました。 –

+1

done https://github.com/joel-costigliola/assertj-core/blob/6983158e5ea2f6fe54c864fc0dd7ec9b672e4653/src/main/java/org/assertj/core/api/ObjectEnumerableAssert.java#L61 –

+0

すばらしいライブラリと迅速なサポートに感謝します。 ! – radistao

関連する問題