Skip to content

Commit a1194fd

Browse files
author
dupirefr
committed
[BAEL-2170] Renamed test and used Collection instead of ArrayList
1 parent cc0749c commit a1194fd

2 files changed

Lines changed: 40 additions & 38 deletions

File tree

core-java-collections/src/test/java/com/baeldung/list/arraylist/ClearVsRemoveAllTest.java

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.list.arraylist;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.Collection;
8+
9+
import static org.junit.Assert.assertEquals;
10+
import static org.junit.Assert.assertTrue;
11+
12+
/**
13+
* Unit tests demonstrating differences between ArrayList#clear() and ArrayList#removeAll()
14+
*/
15+
class ClearVsRemoveAllUnitTest {
16+
17+
/*
18+
* Tests
19+
*/
20+
@Test
21+
void givenArrayListWithElements_whenClear_thenListBecomesEmpty() {
22+
Collection<Integer> collection = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
23+
24+
collection.clear();
25+
26+
assertTrue(collection.isEmpty());
27+
}
28+
29+
@Test
30+
void givenTwoArrayListsWithCommonElements_whenRemoveAll_thenFirstListMissElementsFromSecondList() {
31+
Collection<Integer> firstCollection = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
32+
Collection<Integer> secondCollection = new ArrayList<>(Arrays.asList(3, 4, 5, 6, 7));
33+
34+
firstCollection.removeAll(secondCollection);
35+
36+
assertEquals(Arrays.asList(1, 2), firstCollection);
37+
assertEquals(Arrays.asList(3, 4, 5, 6, 7), secondCollection);
38+
}
39+
40+
}

0 commit comments

Comments
 (0)