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:
		
							parent
							
								
									65cd3b3bd5
								
							
						
					
					
						commit
						ba7efbcc43
					
				| @ -4,6 +4,7 @@ import static org.hamcrest.MatcherAssert.assertThat; | |||||||
| import static org.hamcrest.Matchers.*; | import static org.hamcrest.Matchers.*; | ||||||
| 
 | 
 | ||||||
| import java.util.ArrayList; | import java.util.ArrayList; | ||||||
|  | import java.util.Arrays; | ||||||
| import java.util.LinkedList; | import java.util.LinkedList; | ||||||
| import java.util.List; | import java.util.List; | ||||||
| 
 | 
 | ||||||
| @ -48,4 +49,14 @@ public class RemoveFirstElementUnitTest { | |||||||
|         assertThat(linkedList, not(contains("cat"))); |         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")); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
| } | } | ||||||
|  | |||||||
| @ -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"); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
| } |  | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user