[BAEL-4281] Naming convention and conversion to assertJ

This commit is contained in:
Joao Esperancinha 2020-07-21 06:55:28 +02:00
parent ad1d9b4b84
commit cffc591b5c
5 changed files with 25 additions and 39 deletions

View File

@ -4,30 +4,27 @@ import org.junit.jupiter.api.Test;
import java.util.Arrays;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
public class DeepEqualsCompareUnitTest {
@Test
public void givenArray1andArray2_whenSameContent_thenDeepEquals() {
public void givenSameContents_whenDeepEquals_thenTrue() {
final Plane[][] planes1 = new Plane[][] { new Plane[] { new Plane("Plane 1", "A320") },
new Plane[] { new Plane("Plane 2", "B738") } };
final Plane[][] planes2 = new Plane[][] { new Plane[] { new Plane("Plane 1", "A320") },
new Plane[] { new Plane("Plane 2", "B738") } };
boolean result = Arrays.deepEquals(planes1, planes2);
assertTrue("Result is not true", result);
assertThat(Arrays.deepEquals(planes1, planes2)).isTrue();
}
@Test
public void givenArray1andArray2_whenNotSameContent_thenNotDeepEquals() {
public void givenSameContentsWithDifferentOrder_whenDeepEquals_thenFalse() {
final Plane[][] planes1 = new Plane[][] { new Plane[] { new Plane("Plane 1", "A320") },
new Plane[] { new Plane("Plane 2", "B738") } };
final Plane[][] planes2 = new Plane[][] { new Plane[] { new Plane("Plane 2", "B738") },
new Plane[] { new Plane("Plane 1", "A320") } };
boolean result = Arrays.deepEquals(planes1, planes2);
assertFalse("Result is true", result);
assertThat(Arrays.deepEquals(planes1, planes2)).isFalse();
}
}

View File

@ -4,26 +4,23 @@ import org.junit.jupiter.api.Test;
import java.util.Arrays;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
public class EqualsCompareUnitTest {
@Test
public void givenArray1andArray2_whenSameContent_thenEquals() {
public void givenSameContents_whenEquals_thenTrue() {
final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" };
final String[] planes2 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" };
boolean result = Arrays.equals(planes1, planes2);
assertTrue("Result is not true", result);
assertThat(Arrays.equals(planes1, planes2)).isTrue();
}
@Test
public void givenArray1andArray2_whenSameContentOtherSort_thenNotEquals() {
public void givenSameContentsDifferentOrder_whenEquals_thenFalse() {
final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" };
final String[] planes2 = new String[] { "B738", "A320", "A321", "A319", "B77W", "B737", "A333", "A332" };
boolean result = Arrays.equals(planes1, planes2);
assertFalse("Result is true", result);
assertThat(Arrays.equals(planes1, planes2)).isFalse();
}
}

View File

@ -2,21 +2,17 @@ package com.baeldung.arraycompare;
import org.junit.jupiter.api.Test;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.collection.IsArrayWithSize.arrayWithSize;
import static org.junit.Assert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
public class LengthsCompareUnitTest {
@Test
public void givenArray1andArray2_whenSameSizes_thenSizeEqualsOk() {
public void givenSameContent_whenSizeCompare_thenTrue() {
final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" };
final Integer[] quantities = new Integer[] { 10, 12, 34, 45, 12, 43, 5, 2 };
assertThat(planes1, arrayWithSize(8));
assertThat(quantities, arrayWithSize(8));
assertThat(planes1.length, is(8));
assertThat(quantities.length, is(8));
assertThat(planes1).hasSize(8);
assertThat(quantities).hasSize(8);
}
}

View File

@ -5,11 +5,12 @@ import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.Comparator;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertTrue;
public class OrderCompareUnitTest {
@Test
public void givenArray1andArray2_whenNotSameContent_thenNotDeepEquals() {
public void givenSameContentDifferentOrder_whenSortedAndDeepEquals_thenTrue() {
final Plane[][] planes1 = new Plane[][] {
new Plane[] { new Plane("Plane 1", "A320"), new Plane("Plane 2", "B738") } };
final Plane[][] planes2 = new Plane[][] {
@ -27,7 +28,6 @@ public class OrderCompareUnitTest {
Arrays.sort(planes1[0], planeComparator);
Arrays.sort(planes2[0], planeComparator);
boolean result = Arrays.deepEquals(planes1, planes2);
assertTrue("Result is false", result);
assertThat(Arrays.deepEquals(planes1, planes2)).isTrue();
}
}

View File

@ -2,33 +2,29 @@ package com.baeldung.arraycompare;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.assertj.core.api.Assertions.assertThat;
public class ReferenceCompareUnitTest {
@Test
public void givenArray1andArray2_whenEquals_thenEqual() {
public void givenSameReferences_whenSame_thenTrue() {
final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" };
final String[] planes2 = planes1;
assertSame("Objects are not equal!", planes1, planes2);
assertThat(planes1).isSameAs(planes2);
planes2[0] = "747";
assertSame("Objects are not same!", planes1, planes2);
assertEquals("Objects are not equal!", "747", planes2[0]);
assertEquals("Objects are not equal!", "747", planes1[0]);
assertThat(planes1).isSameAs(planes2);
assertThat(planes2[0]).isEqualTo("747");
assertThat(planes1[0]).isEqualTo("747");
}
@Test
public void givenArray1andArray2_whenDifferentValues_thenNotEqual() {
public void givenSameContentDifferentReferences_whenSame_thenFalse() {
final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" };
final String[] planes2 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" };
assertNotSame("Objects are the same!", planes1, planes2);
assertNotEquals("Objects are equal!", planes1, planes2);
assertThat(planes1).isNotSameAs(planes2);
}
}