Example for removing first element of array (BAEL-2029) (#4970)

* Example for removing first element of array (BAEL-2029)

* Use AssertJ assertions

* Move array test to collections module.

* Remove redundant test
This commit is contained in:
Kevin Wittek 2018-08-19 02:34:47 +02:00 committed by KevinGilmore
parent 65cd3b3bd5
commit ba7efbcc43
2 changed files with 11 additions and 42 deletions

View File

@ -4,6 +4,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
@ -48,4 +49,14 @@ public class RemoveFirstElementUnitTest {
assertThat(linkedList, not(contains("cat")));
}
@Test
public void givenStringArray_whenRemovingFirstElement_thenArrayIsSmallerAndElementRemoved() {
String[] stringArray = {"foo", "bar", "baz"};
String[] modifiedArray = Arrays.copyOfRange(stringArray, 1, stringArray.length);
assertThat(modifiedArray.length, is(2));
assertThat(modifiedArray[0], is("bar"));
}
}

View File

@ -1,42 +0,0 @@
package com.baeldung.array;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
public class RemoveFirstElementUnitTest {
@Test
public void givenStringArray_whenRemovingFirstElement_thenArrayIsSmallerAndElementRemoved() {
String[] stringArray = {"foo", "bar", "baz"};
String[] modifiedArray = Arrays.copyOfRange(stringArray, 1, stringArray.length);
assertThat(modifiedArray.length).isEqualTo(2);
assertThat(modifiedArray[0]).isEqualTo("bar");
}
@Test
public void givenArrayList_whenRemovingFirstElement_thenListSmallerAndElementRemoved() {
List<String> stringList = new ArrayList<>(Arrays.asList("foo", "bar", "baz"));
stringList.remove(0);
assertThat(stringList.size()).isEqualTo(2);
assertThat(stringList.get(0)).isEqualTo("bar");
}
@Test
public void givenLinkedList_whenRemovingFirstElement_thenListSmallerAndElementRemoved() {
List<String> stringList = new LinkedList<>(Arrays.asList("foo", "bar", "baz"));
stringList.remove(0);
assertThat(stringList.size()).isEqualTo(2);
assertThat(stringList.get(0)).isEqualTo("bar");
}
}