BAEL-7108: Comparing Arrays Using compare() Method in Java (#14975)

This commit is contained in:
Azhwani 2023-10-30 17:23:12 +01:00 committed by GitHub
parent 4b29870418
commit de7dadabef
2 changed files with 33 additions and 0 deletions

View File

@ -58,4 +58,8 @@
</plugins>
</build>
<properties>
<maven.compiler.release>11</maven.compiler.release>
</properties>
</project>

View File

@ -0,0 +1,29 @@
package com.baeldung.arraycompare;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import org.junit.jupiter.api.Test;
class ArraysCompareUnitTest {
@Test
void givenSameContents_whenCompare_thenCorrect() {
String[] array1 = new String[] { "A", "B", "C" };
String[] array2 = new String[] { "A", "B", "C" };
assertThat(Arrays.compare(array1, array2)).isEqualTo(0);
}
@Test
void givenDifferentContents_whenCompare_thenDifferent() {
String[] array1 = new String[] { "A", "B", "C" };
String[] array2 = new String[] { "A", "C", "B", "D" };
assertThat(Arrays.compare(array1, array2)).isLessThan(0);
assertThat(Arrays.compare(array2, array1)).isGreaterThan(0);
assertThat(Arrays.compare(array1, null)).isGreaterThan(0);
}
}